Revision 509083 of lang/type

  • Revision slug: Mozilla/Add-ons/SDK/Low-Level_APIs/lang_type
  • Revision title: lang/type
  • Revision id: 509083
  • Created:
  • Creator: wbamberg
  • Is current revision? Yes
  • Comment

Revision Content

Unstable

Functions for working with type detection.

Globals

Functions

isUndefined(value)

Returns true if value is undefined, false otherwise.

let { isUndefined } = require('sdk/lang/type');

var foo;
isUndefined(foo); // true
isUndefined(0); // false
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is undefined.

isNull(value)

Returns true if value is null, false otherwise.

let { isNull } = require('sdk/lang/type');

isNull(null); // true
isNull(false); // false
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is null.

isString(value)

Returns true if value is a String, false otherwise. Uses 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
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is a String.

isNumber(value)

Returns true if value is a Number, false otherwise. Uses 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
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is a Number.

isRegExp(value)

Returns true if value is a RegExp, false otherwise.

let { isRegExp } = require('sdk/lang/type');

isRegExp(/[^\.]*\.js$/); // true
isRegExp(new RegExp('substring')); // true
isRegExp(1000); // false
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is a RegExp.

isDate(value)

Returns true if value is a Date, false otherwise.

let { isDate } = require('sdk/lang/type');

isDate(new Date()); // true
isDate('3/1/2013'); // false
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is a Date.

isFunction(value)

Returns true if value is a Function, false otherwise.

let { isFunction } = require('sdk/lang/type');

let fn = function () {};
isFunction(fn); // true;
isFunction(otherFn); // true;
isFunction(function () {}); // true;

function otherFn () {}
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is a Function.

isObject(value)

Returns true if value is an 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 () {}
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is an Object.

isArray(value)

Returns true if value is an Array, false otherwise. Uses native Array.isArray.

let { isArray } = require('sdk/lang/type');

isArray([]); // true
isArray({}); // false
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is an Array.

isArguments(value)

Returns true if value is an array-like 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);
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is an arguments object.

isPrimitive(value)

Returns true if value is a primitive value: that is, any of null, undefined, number, boolean, or string. Returns false if value is not a primitive value.

let { isPrimitive } = require('sdk/lang/type');

isPrimitive(3); // true
isPrimitive('foo'); // true
isPrimitive({}); // false
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is a primitive.

isFlat(value)

Returns true if value is a direct descendant of Object.prototype or null. Similar to jQuery's isPlainObject.

let { isFlat } = require('sdk/lang/type');

isFlat({}); // true
isFlat(new Type()); // false

function Type () {}
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is a direct descendant of Object.prototype or null.

isEmpty(value)

Returns true if value is an Object with no properties and false otherwise.

let { isEmpty } = require('sdk/lang/type');

isEmpty({}); // true
isEmpty({ init: false }); // false
Parameters

value : object
The variable to check.

Returns

boolean : Boolean indicating if value is an Object with no properties.

isJSON(value)

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
Parameters

value : mixed
The variable to check.

Returns

boolean : Boolean indicating if value is an Array/flat Object containing only atomic values and other flat objects.

instanceOf(value, Type)

Returns true if value is an instance of a given Type. This is similar to the 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() {}
Parameters

value : object
The variable to check.

Type : object
The constructor to compare to value

Returns

boolean : Boolean indicating if value is an instance of Type.

source(value, indent, limit)

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: '@horse_js',
  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]

    }
}
*/
Parameters

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.

Returns

string : The textual representation of value.

Revision Source

<div class="note">
 <p>Unstable</p>
</div>
<p><span class="seoSummary">Functions for working with type detection.</span></p>
<h2 id="Globals">Globals</h2>
<h3 id="Functions">Functions</h3>
<h4 class="addon-sdk-api-name" id="isUndefined(value)"><code>isUndefined(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined"><code>undefined</code></a>, <code>false</code> otherwise.</p>
<pre class="brush: js">
<code>let { isUndefined } = require('sdk/lang/type');</code>

<code>var foo;
isUndefined(foo); // true
isUndefined(0); // false</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is <code>undefined</code>.</p>
<h4 class="addon-sdk-api-name" id="isNull(value)"><code>isNull(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/null"><code>null</code></a>, <code>false</code> otherwise.</p>
<pre class="brush: js">
<code>let { isNull } = require('sdk/lang/type');</code>

isNull(null); // true
isNull(false); // false
</pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is <code>null</code>.</p>
<h4 class="addon-sdk-api-name" id="isString(value)"><code>isString(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is a <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String"><code>String</code></a>, <code>false</code> otherwise. Uses <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof"><code>typeof</code></a> operator to check type, and will only properly detect string primitives: for example, a string created with <code>new String()</code> will always return false.</p>
<pre class="brush: js">
<code>let { isString } = require('sdk/lang/type');</code>

<code>isString('my string'); // true
isString(100); // false
isString('100'); // true</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is a <code>String</code>.</p>
<h4 class="addon-sdk-api-name" id="isNumber(value)"><code>isNumber(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is a <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number"><code>Number</code></a>, <code>false</code> otherwise. Uses <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof"><code>typeof</code></a> operator to check type, and will only properly detect number primitives: for example, a number created with <code>new Number()</code> will always return false.</p>
<pre class="brush: js">
<code>let { isNumber } = require('sdk/lang/type');</code>

<code>isNumber(3.1415); // true
isNumber(100); // true
isNumber('100'); // false</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is a <code>Number</code>.</p>
<h4 class="addon-sdk-api-name" id="isRegExp(value)"><code>isRegExp(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is a <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp"><code>RegExp</code></a>, <code>false</code> otherwise.</p>
<pre class="brush: js">
<code>let { isRegExp } = require('sdk/lang/type');</code>

<code>isRegExp(/[^\.]*\.js$/); // true
isRegExp(new RegExp('substring')); // true
isRegExp(1000); // false</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is a <code>RegExp</code>.</p>
<h4 class="addon-sdk-api-name" id="isDate(value)"><code>isDate(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is a <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date"><code>Date</code></a>, <code>false</code> otherwise.</p>
<pre class="brush: js">
<code>let { isDate } = require('sdk/lang/type');</code>

<code>isDate(new Date()); // true
isDate('3/1/2013'); // false</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is a <code>Date</code>.</p>
<h4 class="addon-sdk-api-name" id="isFunction(value)"><code>isFunction(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is a <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function"><code>Function</code></a>, <code>false</code> otherwise.</p>
<pre class="brush: js">
<code>let { isFunction } = require('sdk/lang/type');</code>

<code>let fn = function () {};
isFunction(fn); // true;
isFunction(otherFn); // true;
isFunction(function () {}); // true;</code>

<code>function otherFn () {}</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is a <code>Function</code>.</p>
<h4 class="addon-sdk-api-name" id="isObject(value)"><code>isObject(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is an <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object"><code>Object</code></a> and not null, <code>false</code> otherwise.</p>
<pre class="brush: js">
<code>let { isObject } = require('sdk/lang/type');</code>

<code>isObject({}); // true
isObject(new Class()); // true
isObject(null); // false
isObject(5); // false</code>

<code>function Class () {}</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is an <code>Object</code>.</p>
<h4 class="addon-sdk-api-name" id="isArray(value)"><code>isArray(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is an <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array"><code>Array</code></a>, <code>false</code> otherwise. Uses native <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/isArray"><code>Array.isArray</code></a>.</p>
<pre class="brush: js">
<code>let { isArray } = require('sdk/lang/type');</code>

<code>isArray([]); // true
isArray({}); // false</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is an <code>Array</code>.</p>
<h4 class="addon-sdk-api-name" id="isArguments(value)"><code>isArguments(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is an array-like <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments"><code>arguments</code></a> object, <code>false</code> otherwise.</p>
<pre class="brush: js">
<code>let { isArguments } = require('sdk/lang/type');</code>

<code>function run () {
  isArguments(arguments); // true
  isArguments([]); // false
  isArguments(Array.slice(arguments)); // false
}
run(1, 2, 3);</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is an <code>arguments</code> object.</p>
<h4 class="addon-sdk-api-name" id="isPrimitive(value)"><code>isPrimitive(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is a primitive value: that is, any of <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/null"><code>null</code></a>, <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined"><code>undefined</code></a>, <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/number"><code>number</code></a>, <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/boolean"><code>boolean</code></a>, or <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/string"><code>string</code></a>. Returns <code>false</code> if <code>value</code> is not a primitive value.</p>
<pre class="brush: js">
<code>let { isPrimitive } = require('sdk/lang/type');</code>

<code>isPrimitive(3); // true
isPrimitive('foo'); // true
isPrimitive({}); // false</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is a primitive.</p>
<h4 class="addon-sdk-api-name" id="isFlat(value)"><code>isFlat(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is a direct descendant of <code>Object.prototype</code> or <code>null</code>. Similar to jQuery's <a href="http://api.jquery.com/jQuery.isPlainObject/"><code>isPlainObject</code></a>.</p>
<pre class="brush: js">
<code>let { isFlat } = require('sdk/lang/type');</code>

<code>isFlat({}); // true
isFlat(new Type()); // false</code>

<code>function Type () {}</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is a direct descendant of <code>Object.prototype</code> or <code>null</code>.</p>
<h4 class="addon-sdk-api-name" id="isEmpty(value)"><code>isEmpty(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is an <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object"><code>Object</code></a> with no properties and <code>false</code> otherwise.</p>
<pre class="brush: js">
<code class="brush: js">let { isEmpty } = require('sdk/lang/type');</code>

<code>isEmpty({}); // true
isEmpty({ init: false }); // false</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : object</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is an <code>Object</code> with no properties.</p>
<h4 class="addon-sdk-api-name" id="isJSON(value)"><code>isJSON(value)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is a string, number, boolean, null, array of JSON-serializable values, or an object whose property values are themselves JSON-serializable. Returns <code>false</code> otherwise.</p>
<pre class="brush: js">
<code>let { isJSON } = require('sdk/lang/type');</code>

<code>isJSON({ value: 42 }); // true
isJSON({ fn: function () {} ); // false</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The variable to check.</p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is an <code>Array</code>/flat <code>Object</code> containing only atomic values and other flat objects.</p>
<h4 class="addon-sdk-api-name" id="instanceOf(value.2C_Type)"><code>instanceOf(value, Type)</code></h4>
<p>Returns <code>true</code> if <code>value</code> is an instance of a given <code>Type</code>. This is similar to the <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/instanceof"><code>instanceof</code></a> operator. The difference is that the <code>Type</code> 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.</p>
<pre class="brush: js">
<code>let { instanceOf } = require('sdk/lang/type');</code>

<code>instanceOf(new Class(), Class); // true
function Class() {}</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : object</strong><br />
 The variable to check.</p>
<p><strong>Type : object</strong><br />
 The constructor to compare to <code>value</code></p>
<h5 id="Returns">Returns</h5>
<p><strong>boolean</strong> : Boolean indicating if <code>value</code> is an instance of <code>Type</code>.</p>
<h4 class="addon-sdk-api-name" id="source(value.2C_indent.2C_limit)"><code>source(value, indent, limit)</code></h4>
<p>Returns the textual representation of <code>value</code>, containing property descriptors and types of properties contained within the object.</p>
<pre class="brush: js">
<code>let { source } = require('sdk/lang/type');</code>

<code>var obj = {
  name: undefined,
  twitter: '@horse_js',
  tweets: [
    { id: 100, text: 'What happens to you if you break the monad laws?' },
    { id: 101, text: 'JAVASCRIPT DUBSTEP GENERATOR' }
  ]
};</code>

<code>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]</code>

<code>            }
        },
        { // [object Object]
            // writable configurable enumerable
            id: 101,
            // writable configurable enumerable
            text: "JAVASCRIPT DUBSTEP GENERATOR",
            "__proto__": { // [object Object]</code>

<code>            }
        }
    ],
    "__proto__": { // [object Object]</code>

<code>    }
}
*/</code></pre>
<h5 id="Parameters">Parameters</h5>
<p><strong>value : mixed</strong><br />
 The source object to create a textual representation of.</p>
<p><strong>indent : string</strong><br />
 Optional. <code>String</code> to be used as indentation in output. 4 spaces by default.</p>
<p><strong>limit : number</strong><br />
 Optional. Number of properties to display per object.</p>
<h5 id="Returns">Returns</h5>
<p><strong>string</strong> : The textual representation of <code>value</code>.</p>
Revert to this revision