boa/value/
type.rs

1use super::JsValue;
2
3/// Possible types of values as defined at <https://tc39.es/ecma262/#sec-typeof-operator>.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub enum Type {
6    Undefined,
7    Null,
8    Boolean,
9    Number,
10    String,
11    Symbol,
12    BigInt,
13    Object,
14}
15
16impl JsValue {
17    /// Get the type of a value
18    ///
19    /// This is the abstract operation Type(v), as described in
20    /// <https://tc39.es/ecma262/multipage/ecmascript-data-types-and-values.html#sec-ecmascript-language-types>.
21    ///
22    /// Check [JsValue::type_of] if you need to call the `typeof` operator.
23    pub fn get_type(&self) -> Type {
24        match *self {
25            Self::Rational(_) | Self::Integer(_) => Type::Number,
26            Self::String(_) => Type::String,
27            Self::Boolean(_) => Type::Boolean,
28            Self::Symbol(_) => Type::Symbol,
29            Self::Null => Type::Null,
30            Self::Undefined => Type::Undefined,
31            Self::BigInt(_) => Type::BigInt,
32            Self::Object(_) => Type::Object,
33        }
34    }
35}