cfx
Version:
programmatically use cfx with node.js
347 lines (255 loc) • 10.4 kB
Markdown
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
The `lang/type` module provides simple helper functions for working with type
detection.
<api name="isUndefined">
Returns `true` if `value` is [`undefined`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined), `false` otherwise.
let { isUndefined } = require('sdk/lang/type');
var foo;
isUndefined(foo); // true
isUndefined(0); // false
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is `undefined`.
</api>
<api name="isNull">
Returns `true` if `value` is [`null`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/null), `false` otherwise.
let { isNull } = require('sdk/lang/type');
isNull(null); // true
isNull(false); // false
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is `null`.
</api>
<api name="isString">
Returns `true` if `value` is a [`String`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String),
`false` otherwise. Uses [`typeof`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof)
operator to check type, and will only properly detect string primitives:
for example, a string created with `new String()` will always return false.
let { isString } = require('sdk/lang/type');
isString('my string'); // true
isString(100); // false
isString('100'); // true
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is a `String`.
</api>
<api name="isNumber">
Returns `true` if `value` is a [`Number`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number),
`false` otherwise. Uses [`typeof`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof)
operator to check type, and will only properly detect number primitives:
for example, a number created with `new Number()` will always return false.
let { isNumber } = require('sdk/lang/type');
isNumber(3.1415); // true
isNumber(100); // true
isNumber('100'); // false
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is a `Number`.
</api>
<api name="isRegExp">
Returns `true` if `value` is a [`RegExp`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp), `false` otherwise.
let { isRegExp } = require('sdk/lang/type');
isRegExp(/[^\.]*\.js$/); // true
isRegExp(new RegExp('substring')); // true
isRegExp(1000); // false
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is a `RegExp`.
</api>
<api name="isDate">
Returns `true` if `value` is a [`Date`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date), `false` otherwise.
let { isDate } = require('sdk/lang/type');
isDate(new Date()); // true
isDate('3/1/2013'); // false
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is a `Date`.
</api>
<api name="isFunction">
Returns `true` if `value` is a [`Function`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function), `false` otherwise.
let { isFunction } = require('sdk/lang/type');
let fn = function () {};
isFunction(fn); // true;
isFunction(otherFn); // true;
isFunction(function () {}); // true;
function otherFn () {}
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is a `Function`.
</api>
<api name="isObject">
Returns `true` if `value` is an [`Object`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object) and not null, `false` otherwise.
let { isObject } = require('sdk/lang/type');
isObject({}); // true
isObject(new Class()); // true
isObject(null); // false
isObject(5); // false
function Class () {}
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is an `Object`.
</api>
<api name="isArray">
Returns `true` if `value` is an [`Array`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array), `false` otherwise. Uses native
[`Array.isArray`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/isArray).
let { isArray } = require('sdk/lang/type');
isArray([]); // true
isArray({}); // false
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is an `Array`.
</api>
<api name="isArguments">
Returns `true` if `value` is an array-like [`arguments`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments) object,
`false` otherwise.
let { isArguments } = require('sdk/lang/type');
function run () {
isArguments(arguments); // true
isArguments([]); // false
isArguments(Array.slice(arguments)); // false
}
run(1, 2, 3);
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is an `arguments` object.
</api>
<api name="isPrimitive">
Returns `true` if `value` is a primitive value: that is, any of [`null`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/null), [`undefined`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined), [`number`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/number),
[`boolean`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/boolean), or [`string`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/string). Returns `false` if `value` is not a primitive value.
let { isPrimitive } = require('sdk/lang/type');
isPrimitive(3); // true
isPrimitive('foo'); // true
isPrimitive({}); // false
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is a primitive.
</api>
<api name="isFlat">
Returns `true` if `value` is a direct descendant of `Object.prototype` or `null`.
Similar to jQuery's [`isPlainObject`](http://api.jquery.com/jQuery.isPlainObject/).
let { isFlat } = require('sdk/lang/type');
isFlat({}); // true
isFlat(new Type()); // false
function Type () {}
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is a direct descendant of `Object.prototype` or `null`.
</api>
<api name="isEmpty">
Returns `true` if `value` is an [`Object`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object) with no properties and `false` otherwise.
let { isEmpty } = require('sdk/lang/type');
isEmpty({}); // true
isEmpty({ init: false }); // false
value {object}
The variable to check.
{boolean}
Boolean indicating if `value` is an `Object` with no properties.
</api>
<api name="isJSON">
Returns `true` if `value` is a string, number, boolean, null, array of JSON-serializable values, or an object whose property values are themselves JSON-serializable. Returns `false` otherwise.
let { isJSON } = require('sdk/lang/type');
isJSON({ value: 42 }); // true
isJSON({ fn: function () {} ); // false
value {mixed}
The variable to check.
{boolean}
Boolean indicating if `value` is an `Array`/flat `Object` containing only
atomic values and other flat objects.
</api>
<api name="instanceOf">
Returns `true` if `value` is an instance of a given `Type`. This is similar to the [`instanceof`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/instanceof) operator.
The difference is that the `Type` constructor can be from a scope that has
a different top level object: for example, it could be from a different iframe,
module or sandbox.
let { instanceOf } = require('sdk/lang/type');
instanceOf(new Class(), Class); // true
function Class() {}
value {object}
The variable to check.
Type {object}
The constructor to compare to `value`
{boolean}
Boolean indicating if `value` is an instance of `Type`.
</api>
<api name="source">
Returns the textual representation of `value`, containing property descriptors and types
of properties contained within the object.
let { source } = require('sdk/lang/type');
var obj = {
name: undefined,
twitter: '',
tweets: [
{ id: 100, text: 'What happens to you if you break the monad laws?' },
{ id: 101, text: 'JAVASCRIPT DUBSTEP GENERATOR' }
]
};
console.log(source(obj));
// Prints the below
/*
{ // [object Object]
// writable configurable enumerable
name: undefined,
// writable configurable enumerable
twitter: "@horse_js",
// writable configurable enumerable
tweets: [
{ // [object Object]
// writable configurable enumerable
id: 100,
// writable configurable enumerable
text: "What happens to you if you break the monad laws?",
"__proto__": { // [object Object]
}
},
{ // [object Object]
// writable configurable enumerable
id: 101,
// writable configurable enumerable
text: "JAVASCRIPT DUBSTEP GENERATOR",
"__proto__": { // [object Object]
}
}
],
"__proto__": { // [object Object]
}
}
*/
value {mixed}
The source object to create a textual representation of.
indent {string}
Optional. `String` to be used as indentation in output. 4 spaces by default.
limit {number}
Optional. Number of properties to display per object.
{string}
The textual representation of `value`.
</api>