Comparing version 2.4.5 to 2.4.6
@@ -11,6 +11,2 @@ 'use strict'; | ||
var _core2 = _interopRequireDefault(_core); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -128,3 +124,3 @@ | ||
var sliced = []; | ||
var end = _core2.default.isDef(opt_end) ? opt_end : arr.length; | ||
var end = (0, _core.isDef)(opt_end) ? opt_end : arr.length; | ||
for (var i = start; i < end; i++) { | ||
@@ -131,0 +127,0 @@ sliced.push(arr[i]); |
609
lib/core.js
'use strict'; | ||
/** | ||
* A collection of core utility functions. | ||
* @const | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
value: true | ||
}); | ||
@@ -9,368 +14,302 @@ | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
exports.abstractMethod = abstractMethod; | ||
exports.collectSuperClassesProperty = collectSuperClassesProperty; | ||
exports.disableCompatibilityMode = disableCompatibilityMode; | ||
exports.enableCompatibilityMode = enableCompatibilityMode; | ||
exports.getCompatibilityModeData = getCompatibilityModeData; | ||
exports.getFunctionName = getFunctionName; | ||
exports.getUid = getUid; | ||
exports.identityFunction = identityFunction; | ||
exports.isBoolean = isBoolean; | ||
exports.isDef = isDef; | ||
exports.isDefAndNotNull = isDefAndNotNull; | ||
exports.isDocument = isDocument; | ||
exports.isElement = isElement; | ||
exports.isFunction = isFunction; | ||
exports.isNull = isNull; | ||
exports.isNumber = isNumber; | ||
exports.isWindow = isWindow; | ||
exports.isObject = isObject; | ||
exports.isPromise = isPromise; | ||
exports.isString = isString; | ||
exports.mergeSuperClassesProperty = mergeSuperClassesProperty; | ||
exports.nullFunction = nullFunction; | ||
var compatibilityModeData_ = void 0; | ||
/** | ||
* A collection of core utility functions. | ||
* @const | ||
* Counter for unique id. | ||
* @type {Number} | ||
* @private | ||
*/ | ||
var uniqueIdCounter_ = 1; | ||
var core = function () { | ||
function core() { | ||
_classCallCheck(this, core); | ||
} | ||
/** | ||
* Unique id property prefix. | ||
* @type {String} | ||
* @protected | ||
*/ | ||
var UID_PROPERTY = exports.UID_PROPERTY = 'core_' + (Math.random() * 1e9 >>> 0); | ||
_createClass(core, null, [{ | ||
key: 'abstractMethod', | ||
/** | ||
* When defining a class Foo with an abstract method bar(), you can do: | ||
* Foo.prototype.bar = abstractMethod | ||
* | ||
* Now if a subclass of Foo fails to override bar(), an error will be thrown | ||
* when bar() is invoked. | ||
* | ||
* @type {!Function} | ||
* @throws {Error} when invoked to indicate the method should be overridden. | ||
*/ | ||
function abstractMethod() { | ||
throw Error('Unimplemented abstract method'); | ||
} | ||
/** | ||
* When defining a class Foo with an abstract method bar(), you can do: | ||
* Foo.prototype.bar = core.abstractMethod | ||
* | ||
* Now if a subclass of Foo fails to override bar(), an error will be thrown | ||
* when bar() is invoked. | ||
* | ||
* @type {!Function} | ||
* @throws {Error} when invoked to indicate the method should be overridden. | ||
*/ | ||
value: function abstractMethod() { | ||
throw Error('Unimplemented abstract method'); | ||
} | ||
/** | ||
* Loops constructor super classes collecting its properties values. If | ||
* property is not available on the super class `undefined` will be | ||
* collected as value for the class hierarchy position. | ||
* @param {!function()} constructor Class constructor. | ||
* @param {string} propertyName Property name to be collected. | ||
* @return {Array.<*>} Array of collected values. | ||
* TODO(*): Rethink superclass loop. | ||
*/ | ||
function collectSuperClassesProperty(constructor, propertyName) { | ||
var propertyValues = [constructor[propertyName]]; | ||
while (constructor.__proto__ && !constructor.__proto__.isPrototypeOf(Function)) { | ||
constructor = constructor.__proto__; | ||
propertyValues.push(constructor[propertyName]); | ||
} | ||
return propertyValues; | ||
} | ||
/** | ||
* Loops constructor super classes collecting its properties values. If | ||
* property is not available on the super class `undefined` will be | ||
* collected as value for the class hierarchy position. | ||
* @param {!function()} constructor Class constructor. | ||
* @param {string} propertyName Property name to be collected. | ||
* @return {Array.<*>} Array of collected values. | ||
* TODO(*): Rethink superclass loop. | ||
*/ | ||
/** | ||
* Disables Metal.js's compatibility mode. | ||
*/ | ||
function disableCompatibilityMode() { | ||
compatibilityModeData_ = null; | ||
} | ||
}, { | ||
key: 'collectSuperClassesProperty', | ||
value: function collectSuperClassesProperty(constructor, propertyName) { | ||
var propertyValues = [constructor[propertyName]]; | ||
while (constructor.__proto__ && !constructor.__proto__.isPrototypeOf(Function)) { | ||
constructor = constructor.__proto__; | ||
propertyValues.push(constructor[propertyName]); | ||
} | ||
return propertyValues; | ||
} | ||
/** | ||
* Enables Metal.js's compatibility mode with the following features from rc | ||
* and 1.x versions: | ||
* - Using "key" to reference component instances. In the current version | ||
* this should be done via "ref" instead. This allows old code still | ||
* using "key" to keep working like before. NOTE: this may cause | ||
* problems, since "key" is meant to be used differently. Only use this | ||
* if it's not possible to upgrade the code to use "ref" instead. | ||
* @param {Object=} opt_data Optional object with data to specify more | ||
* details, such as: | ||
* - renderers {Array} the template renderers that should be in | ||
* compatibility mode, either their constructors or strings | ||
* representing them (e.g. 'soy' or 'jsx'). By default, all the ones | ||
* that extend from IncrementalDomRenderer. | ||
* @type {Object} | ||
*/ | ||
function enableCompatibilityMode() { | ||
var opt_data = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
/** | ||
* Disables Metal.js's compatibility mode. | ||
*/ | ||
compatibilityModeData_ = opt_data; | ||
} | ||
}, { | ||
key: 'disableCompatibilityMode', | ||
value: function disableCompatibilityMode() { | ||
compatibilityModeData_ = null; | ||
} | ||
/** | ||
* Returns the data used for compatibility mode, or nothing if it hasn't been | ||
* enabled. | ||
* @return {Object} | ||
*/ | ||
function getCompatibilityModeData() { | ||
// Compatibility mode can be set via the __METAL_COMPATIBILITY__ global var. | ||
if (!compatibilityModeData_) { | ||
if (typeof window !== 'undefined' && window.__METAL_COMPATIBILITY__) { | ||
enableCompatibilityMode(window.__METAL_COMPATIBILITY__); | ||
} | ||
} | ||
return compatibilityModeData_; | ||
} | ||
/** | ||
* Enables Metal.js's compatibility mode with the following features from rc | ||
* and 1.x versions: | ||
* - Using "key" to reference component instances. In the current version | ||
* this should be done via "ref" instead. This allows old code still | ||
* using "key" to keep working like before. NOTE: this may cause | ||
* problems, since "key" is meant to be used differently. Only use this | ||
* if it's not possible to upgrade the code to use "ref" instead. | ||
* @param {Object=} opt_data Optional object with data to specify more | ||
* details, such as: | ||
* - renderers {Array} the template renderers that should be in | ||
* compatibility mode, either their constructors or strings | ||
* representing them (e.g. 'soy' or 'jsx'). By default, all the ones | ||
* that extend from IncrementalDomRenderer. | ||
* @type {Object} | ||
*/ | ||
/** | ||
* Gets the name of the given function. If the current browser doesn't | ||
* support the `name` property, this will calculate it from the function's | ||
* content string. | ||
* @param {!function()} fn | ||
* @return {string} | ||
*/ | ||
function getFunctionName(fn) { | ||
if (!fn.name) { | ||
var str = fn.toString(); | ||
fn.name = str.substring(9, str.indexOf('(')); | ||
} | ||
return fn.name; | ||
} | ||
}, { | ||
key: 'enableCompatibilityMode', | ||
value: function enableCompatibilityMode() { | ||
var opt_data = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
/** | ||
* Gets an unique id. If `opt_object` argument is passed, the object is | ||
* mutated with an unique id. Consecutive calls with the same object | ||
* reference won't mutate the object again, instead the current object uid | ||
* returns. See {@link UID_PROPERTY}. | ||
* @param {Object=} opt_object Optional object to be mutated with the uid. If | ||
* not specified this method only returns the uid. | ||
* @param {boolean=} opt_noInheritance Optional flag indicating if this | ||
* object's uid property can be inherited from parents or not. | ||
* @throws {Error} when invoked to indicate the method should be overridden. | ||
*/ | ||
function getUid(opt_object, opt_noInheritance) { | ||
if (opt_object) { | ||
var id = opt_object[UID_PROPERTY]; | ||
if (opt_noInheritance && !opt_object.hasOwnProperty(UID_PROPERTY)) { | ||
id = null; | ||
} | ||
return id || (opt_object[UID_PROPERTY] = uniqueIdCounter_++); | ||
} | ||
return uniqueIdCounter_++; | ||
} | ||
compatibilityModeData_ = opt_data; | ||
} | ||
/** | ||
* The identity function. Returns its first argument. | ||
* @param {*=} opt_returnValue The single value that will be returned. | ||
* @return {?} The first argument. | ||
*/ | ||
function identityFunction(opt_returnValue) { | ||
return opt_returnValue; | ||
} | ||
/** | ||
* Returns the data used for compatibility mode, or nothing if it hasn't been | ||
* enabled. | ||
* @return {Object} | ||
*/ | ||
/** | ||
* Returns true if the specified value is a boolean. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is boolean. | ||
*/ | ||
function isBoolean(val) { | ||
return typeof val === 'boolean'; | ||
} | ||
}, { | ||
key: 'getCompatibilityModeData', | ||
value: function getCompatibilityModeData() { | ||
// Compatibility mode can be set via the __METAL_COMPATIBILITY__ global var. | ||
if (!compatibilityModeData_) { | ||
if (typeof window !== 'undefined' && window.__METAL_COMPATIBILITY__) { | ||
core.enableCompatibilityMode(window.__METAL_COMPATIBILITY__); | ||
} | ||
} | ||
return compatibilityModeData_; | ||
} | ||
/** | ||
* Returns true if the specified value is not undefined. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is defined. | ||
*/ | ||
function isDef(val) { | ||
return val !== undefined; | ||
} | ||
/** | ||
* Gets the name of the given function. If the current browser doesn't | ||
* support the `name` property, this will calculate it from the function's | ||
* content string. | ||
* @param {!function()} fn | ||
* @return {string} | ||
*/ | ||
/** | ||
* Returns true if value is not undefined or null. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
function isDefAndNotNull(val) { | ||
return isDef(val) && !isNull(val); | ||
} | ||
}, { | ||
key: 'getFunctionName', | ||
value: function getFunctionName(fn) { | ||
if (!fn.name) { | ||
var str = fn.toString(); | ||
fn.name = str.substring(9, str.indexOf('(')); | ||
} | ||
return fn.name; | ||
} | ||
/** | ||
* Returns true if value is a document. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
function isDocument(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && val.nodeType === 9; | ||
} | ||
/** | ||
* Gets an unique id. If `opt_object` argument is passed, the object is | ||
* mutated with an unique id. Consecutive calls with the same object | ||
* reference won't mutate the object again, instead the current object uid | ||
* returns. See {@link core.UID_PROPERTY}. | ||
* @param {Object=} opt_object Optional object to be mutated with the uid. If | ||
* not specified this method only returns the uid. | ||
* @param {boolean=} opt_noInheritance Optional flag indicating if this | ||
* object's uid property can be inherited from parents or not. | ||
* @throws {Error} when invoked to indicate the method should be overridden. | ||
*/ | ||
/** | ||
* Returns true if value is a dom element. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
function isElement(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && val.nodeType === 1; | ||
} | ||
}, { | ||
key: 'getUid', | ||
value: function getUid(opt_object, opt_noInheritance) { | ||
if (opt_object) { | ||
var id = opt_object[core.UID_PROPERTY]; | ||
if (opt_noInheritance && !opt_object.hasOwnProperty(core.UID_PROPERTY)) { | ||
id = null; | ||
} | ||
return id || (opt_object[core.UID_PROPERTY] = core.uniqueIdCounter_++); | ||
} | ||
return core.uniqueIdCounter_++; | ||
} | ||
/** | ||
* Returns true if the specified value is a function. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is a function. | ||
*/ | ||
function isFunction(val) { | ||
return typeof val === 'function'; | ||
} | ||
/** | ||
* The identity function. Returns its first argument. | ||
* @param {*=} opt_returnValue The single value that will be returned. | ||
* @return {?} The first argument. | ||
*/ | ||
/** | ||
* Returns true if value is null. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
function isNull(val) { | ||
return val === null; | ||
} | ||
}, { | ||
key: 'identityFunction', | ||
value: function identityFunction(opt_returnValue) { | ||
return opt_returnValue; | ||
} | ||
/** | ||
* Returns true if the specified value is a number. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is a number. | ||
*/ | ||
function isNumber(val) { | ||
return typeof val === 'number'; | ||
} | ||
/** | ||
* Returns true if the specified value is a boolean. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is boolean. | ||
*/ | ||
/** | ||
* Returns true if value is a window. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
function isWindow(val) { | ||
return val !== null && val === val.window; | ||
} | ||
}, { | ||
key: 'isBoolean', | ||
value: function isBoolean(val) { | ||
return typeof val === 'boolean'; | ||
} | ||
/** | ||
* Returns true if the specified value is an object. This includes arrays | ||
* and functions. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is an object. | ||
*/ | ||
function isObject(val) { | ||
var type = typeof val === 'undefined' ? 'undefined' : _typeof(val); | ||
return type === 'object' && val !== null || type === 'function'; | ||
} | ||
/** | ||
* Returns true if the specified value is not undefined. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is defined. | ||
*/ | ||
/** | ||
* Returns true if value is a Promise. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
function isPromise(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && typeof val.then === 'function'; | ||
} | ||
}, { | ||
key: 'isDef', | ||
value: function isDef(val) { | ||
return val !== undefined; | ||
} | ||
/** | ||
* Returns true if value is a string. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
function isString(val) { | ||
return typeof val === 'string' || val instanceof String; | ||
} | ||
/** | ||
* Returns true if value is not undefined or null. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
}, { | ||
key: 'isDefAndNotNull', | ||
value: function isDefAndNotNull(val) { | ||
return core.isDef(val) && !core.isNull(val); | ||
} | ||
/** | ||
* Returns true if value is a document. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
}, { | ||
key: 'isDocument', | ||
value: function isDocument(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && val.nodeType === 9; | ||
} | ||
/** | ||
* Returns true if value is a dom element. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
}, { | ||
key: 'isElement', | ||
value: function isElement(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && val.nodeType === 1; | ||
} | ||
/** | ||
* Returns true if the specified value is a function. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is a function. | ||
*/ | ||
}, { | ||
key: 'isFunction', | ||
value: function isFunction(val) { | ||
return typeof val === 'function'; | ||
} | ||
/** | ||
* Returns true if value is null. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
}, { | ||
key: 'isNull', | ||
value: function isNull(val) { | ||
return val === null; | ||
} | ||
/** | ||
* Returns true if the specified value is a number. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is a number. | ||
*/ | ||
}, { | ||
key: 'isNumber', | ||
value: function isNumber(val) { | ||
return typeof val === 'number'; | ||
} | ||
/** | ||
* Returns true if value is a window. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
}, { | ||
key: 'isWindow', | ||
value: function isWindow(val) { | ||
return val !== null && val === val.window; | ||
} | ||
/** | ||
* Returns true if the specified value is an object. This includes arrays | ||
* and functions. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is an object. | ||
*/ | ||
}, { | ||
key: 'isObject', | ||
value: function isObject(val) { | ||
var type = typeof val === 'undefined' ? 'undefined' : _typeof(val); | ||
return type === 'object' && val !== null || type === 'function'; | ||
} | ||
/** | ||
* Returns true if value is a Promise. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
}, { | ||
key: 'isPromise', | ||
value: function isPromise(val) { | ||
return val && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && typeof val.then === 'function'; | ||
} | ||
/** | ||
* Returns true if value is a string. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
}, { | ||
key: 'isString', | ||
value: function isString(val) { | ||
return typeof val === 'string' || val instanceof String; | ||
} | ||
/** | ||
* Merges the values of a static property a class with the values of that | ||
* property for all its super classes, and stores it as a new static | ||
* property of that class. If the static property already existed, it won't | ||
* be recalculated. | ||
* @param {!function()} constructor Class constructor. | ||
* @param {string} propertyName Property name to be collected. | ||
* @param {function(*, *):*=} opt_mergeFn Function that receives an array filled | ||
* with the values of the property for the current class and all its super classes. | ||
* Should return the merged value to be stored on the current class. | ||
* @return {boolean} Returns true if merge happens, false otherwise. | ||
*/ | ||
}, { | ||
key: 'mergeSuperClassesProperty', | ||
value: function mergeSuperClassesProperty(constructor, propertyName, opt_mergeFn) { | ||
var mergedName = propertyName + '_MERGED'; | ||
if (constructor.hasOwnProperty(mergedName)) { | ||
return false; | ||
} | ||
var merged = core.collectSuperClassesProperty(constructor, propertyName); | ||
if (opt_mergeFn) { | ||
merged = opt_mergeFn(merged); | ||
} | ||
constructor[mergedName] = merged; | ||
return true; | ||
} | ||
/** | ||
* Null function used for default values of callbacks, etc. | ||
* @return {void} Nothing. | ||
*/ | ||
}, { | ||
key: 'nullFunction', | ||
value: function nullFunction() {} | ||
}]); | ||
return core; | ||
}(); | ||
/** | ||
* Unique id property prefix. | ||
* @type {String} | ||
* @protected | ||
* Merges the values of a export function property a class with the values of that | ||
* property for all its super classes, and stores it as a new static | ||
* property of that class. If the export function property already existed, it won't | ||
* be recalculated. | ||
* @param {!function()} constructor Class constructor. | ||
* @param {string} propertyName Property name to be collected. | ||
* @param {function(*, *):*=} opt_mergeFn Function that receives an array filled | ||
* with the values of the property for the current class and all its super classes. | ||
* Should return the merged value to be stored on the current class. | ||
* @return {boolean} Returns true if merge happens, false otherwise. | ||
*/ | ||
function mergeSuperClassesProperty(constructor, propertyName, opt_mergeFn) { | ||
var mergedName = propertyName + '_MERGED'; | ||
if (constructor.hasOwnProperty(mergedName)) { | ||
return false; | ||
} | ||
var merged = collectSuperClassesProperty(constructor, propertyName); | ||
if (opt_mergeFn) { | ||
merged = opt_mergeFn(merged); | ||
} | ||
constructor[mergedName] = merged; | ||
return true; | ||
} | ||
core.UID_PROPERTY = 'core_' + (Math.random() * 1e9 >>> 0); | ||
/** | ||
* Counter for unique id. | ||
* @type {Number} | ||
* @private | ||
* Null function used for default values of callbacks, etc. | ||
* @return {void} Nothing. | ||
*/ | ||
core.uniqueIdCounter_ = 1; | ||
exports.default = core; | ||
function nullFunction() {} |
@@ -6,8 +6,18 @@ 'use strict'; | ||
}); | ||
exports.string = exports.object = exports.Disposable = exports.async = exports.array = exports.core = undefined; | ||
exports.core = exports.string = exports.object = exports.Disposable = exports.async = exports.array = undefined; | ||
var _core = require('./core'); | ||
var _core2 = _interopRequireDefault(_core); | ||
Object.keys(_core).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _core[key]; | ||
} | ||
}); | ||
}); | ||
var core = _interopRequireWildcard(_core); | ||
var _array = require('./array/array'); | ||
@@ -35,4 +45,4 @@ | ||
exports.default = _core2.default; | ||
exports.core = _core2.default; | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
exports.array = _array2.default; | ||
@@ -42,2 +52,10 @@ exports.async = _async2.default; | ||
exports.object = _object2.default; | ||
exports.string = _string2.default; | ||
exports.string = _string2.default; | ||
// This is for backwards compatibility, making sure that old imports for the | ||
// "core" object still work. It's best to use the named exports for each | ||
// function instead though, since that allows bundlers like Rollup to reduce the | ||
// bundle size by removing unused code. | ||
exports.default = core; | ||
exports.core = core; |
{ | ||
"name": "metal", | ||
"version": "2.4.5", | ||
"version": "2.4.6", | ||
"description": "Core functions from Metal.js, with utilities for dealing with arrays, objects and others.", | ||
@@ -5,0 +5,0 @@ "license": "BSD-3-Clause", |
'use strict'; | ||
import core from '../core'; | ||
import { isDef } from '../core'; | ||
@@ -93,3 +93,3 @@ class array { | ||
var sliced = []; | ||
var end = core.isDef(opt_end) ? opt_end : arr.length; | ||
var end = isDef(opt_end) ? opt_end : arr.length; | ||
for (var i = start; i < end; i++) { | ||
@@ -96,0 +96,0 @@ sliced.push(arr[i]); |
491
src/core.js
'use strict'; | ||
let compatibilityModeData_; | ||
/** | ||
@@ -9,280 +7,279 @@ * A collection of core utility functions. | ||
*/ | ||
class core { | ||
/** | ||
* When defining a class Foo with an abstract method bar(), you can do: | ||
* Foo.prototype.bar = core.abstractMethod | ||
* | ||
* Now if a subclass of Foo fails to override bar(), an error will be thrown | ||
* when bar() is invoked. | ||
* | ||
* @type {!Function} | ||
* @throws {Error} when invoked to indicate the method should be overridden. | ||
*/ | ||
static abstractMethod() { | ||
throw Error('Unimplemented abstract method'); | ||
} | ||
/** | ||
* Loops constructor super classes collecting its properties values. If | ||
* property is not available on the super class `undefined` will be | ||
* collected as value for the class hierarchy position. | ||
* @param {!function()} constructor Class constructor. | ||
* @param {string} propertyName Property name to be collected. | ||
* @return {Array.<*>} Array of collected values. | ||
* TODO(*): Rethink superclass loop. | ||
*/ | ||
static collectSuperClassesProperty(constructor, propertyName) { | ||
var propertyValues = [constructor[propertyName]]; | ||
while (constructor.__proto__ && !constructor.__proto__.isPrototypeOf(Function)) { | ||
constructor = constructor.__proto__; | ||
propertyValues.push(constructor[propertyName]); | ||
} | ||
return propertyValues; | ||
} | ||
let compatibilityModeData_; | ||
/** | ||
* Disables Metal.js's compatibility mode. | ||
*/ | ||
static disableCompatibilityMode() { | ||
compatibilityModeData_ = null; | ||
} | ||
/** | ||
* Counter for unique id. | ||
* @type {Number} | ||
* @private | ||
*/ | ||
let uniqueIdCounter_ = 1; | ||
/** | ||
* Enables Metal.js's compatibility mode with the following features from rc | ||
* and 1.x versions: | ||
* - Using "key" to reference component instances. In the current version | ||
* this should be done via "ref" instead. This allows old code still | ||
* using "key" to keep working like before. NOTE: this may cause | ||
* problems, since "key" is meant to be used differently. Only use this | ||
* if it's not possible to upgrade the code to use "ref" instead. | ||
* @param {Object=} opt_data Optional object with data to specify more | ||
* details, such as: | ||
* - renderers {Array} the template renderers that should be in | ||
* compatibility mode, either their constructors or strings | ||
* representing them (e.g. 'soy' or 'jsx'). By default, all the ones | ||
* that extend from IncrementalDomRenderer. | ||
* @type {Object} | ||
*/ | ||
static enableCompatibilityMode(opt_data = {}) { | ||
compatibilityModeData_ = opt_data; | ||
} | ||
/** | ||
* Unique id property prefix. | ||
* @type {String} | ||
* @protected | ||
*/ | ||
export const UID_PROPERTY = 'core_' + ((Math.random() * 1e9) >>> 0); | ||
/** | ||
* Returns the data used for compatibility mode, or nothing if it hasn't been | ||
* enabled. | ||
* @return {Object} | ||
*/ | ||
static getCompatibilityModeData() { | ||
// Compatibility mode can be set via the __METAL_COMPATIBILITY__ global var. | ||
if (!compatibilityModeData_) { | ||
if (typeof window !== 'undefined' && window.__METAL_COMPATIBILITY__) { | ||
core.enableCompatibilityMode(window.__METAL_COMPATIBILITY__); | ||
} | ||
} | ||
return compatibilityModeData_; | ||
} | ||
/** | ||
* When defining a class Foo with an abstract method bar(), you can do: | ||
* Foo.prototype.bar = abstractMethod | ||
* | ||
* Now if a subclass of Foo fails to override bar(), an error will be thrown | ||
* when bar() is invoked. | ||
* | ||
* @type {!Function} | ||
* @throws {Error} when invoked to indicate the method should be overridden. | ||
*/ | ||
export function abstractMethod() { | ||
throw Error('Unimplemented abstract method'); | ||
} | ||
/** | ||
* Gets the name of the given function. If the current browser doesn't | ||
* support the `name` property, this will calculate it from the function's | ||
* content string. | ||
* @param {!function()} fn | ||
* @return {string} | ||
*/ | ||
static getFunctionName(fn) { | ||
if (!fn.name) { | ||
var str = fn.toString(); | ||
fn.name = str.substring(9, str.indexOf('(')); | ||
} | ||
return fn.name; | ||
/** | ||
* Loops constructor super classes collecting its properties values. If | ||
* property is not available on the super class `undefined` will be | ||
* collected as value for the class hierarchy position. | ||
* @param {!function()} constructor Class constructor. | ||
* @param {string} propertyName Property name to be collected. | ||
* @return {Array.<*>} Array of collected values. | ||
* TODO(*): Rethink superclass loop. | ||
*/ | ||
export function collectSuperClassesProperty(constructor, propertyName) { | ||
var propertyValues = [constructor[propertyName]]; | ||
while (constructor.__proto__ && !constructor.__proto__.isPrototypeOf(Function)) { | ||
constructor = constructor.__proto__; | ||
propertyValues.push(constructor[propertyName]); | ||
} | ||
return propertyValues; | ||
} | ||
/** | ||
* Gets an unique id. If `opt_object` argument is passed, the object is | ||
* mutated with an unique id. Consecutive calls with the same object | ||
* reference won't mutate the object again, instead the current object uid | ||
* returns. See {@link core.UID_PROPERTY}. | ||
* @param {Object=} opt_object Optional object to be mutated with the uid. If | ||
* not specified this method only returns the uid. | ||
* @param {boolean=} opt_noInheritance Optional flag indicating if this | ||
* object's uid property can be inherited from parents or not. | ||
* @throws {Error} when invoked to indicate the method should be overridden. | ||
*/ | ||
static getUid(opt_object, opt_noInheritance) { | ||
if (opt_object) { | ||
var id = opt_object[core.UID_PROPERTY]; | ||
if (opt_noInheritance && !opt_object.hasOwnProperty(core.UID_PROPERTY)) { | ||
id = null; | ||
} | ||
return id || (opt_object[core.UID_PROPERTY] = core.uniqueIdCounter_++); | ||
} | ||
return core.uniqueIdCounter_++; | ||
} | ||
/** | ||
* Disables Metal.js's compatibility mode. | ||
*/ | ||
export function disableCompatibilityMode() { | ||
compatibilityModeData_ = null; | ||
} | ||
/** | ||
* The identity function. Returns its first argument. | ||
* @param {*=} opt_returnValue The single value that will be returned. | ||
* @return {?} The first argument. | ||
*/ | ||
static identityFunction(opt_returnValue) { | ||
return opt_returnValue; | ||
} | ||
/** | ||
* Enables Metal.js's compatibility mode with the following features from rc | ||
* and 1.x versions: | ||
* - Using "key" to reference component instances. In the current version | ||
* this should be done via "ref" instead. This allows old code still | ||
* using "key" to keep working like before. NOTE: this may cause | ||
* problems, since "key" is meant to be used differently. Only use this | ||
* if it's not possible to upgrade the code to use "ref" instead. | ||
* @param {Object=} opt_data Optional object with data to specify more | ||
* details, such as: | ||
* - renderers {Array} the template renderers that should be in | ||
* compatibility mode, either their constructors or strings | ||
* representing them (e.g. 'soy' or 'jsx'). By default, all the ones | ||
* that extend from IncrementalDomRenderer. | ||
* @type {Object} | ||
*/ | ||
export function enableCompatibilityMode(opt_data = {}) { | ||
compatibilityModeData_ = opt_data; | ||
} | ||
/** | ||
* Returns true if the specified value is a boolean. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is boolean. | ||
*/ | ||
static isBoolean(val) { | ||
return typeof val === 'boolean'; | ||
/** | ||
* Returns the data used for compatibility mode, or nothing if it hasn't been | ||
* enabled. | ||
* @return {Object} | ||
*/ | ||
export function getCompatibilityModeData() { | ||
// Compatibility mode can be set via the __METAL_COMPATIBILITY__ global var. | ||
if (!compatibilityModeData_) { | ||
if (typeof window !== 'undefined' && window.__METAL_COMPATIBILITY__) { | ||
enableCompatibilityMode(window.__METAL_COMPATIBILITY__); | ||
} | ||
} | ||
return compatibilityModeData_; | ||
} | ||
/** | ||
* Returns true if the specified value is not undefined. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is defined. | ||
*/ | ||
static isDef(val) { | ||
return val !== undefined; | ||
/** | ||
* Gets the name of the given function. If the current browser doesn't | ||
* support the `name` property, this will calculate it from the function's | ||
* content string. | ||
* @param {!function()} fn | ||
* @return {string} | ||
*/ | ||
export function getFunctionName(fn) { | ||
if (!fn.name) { | ||
var str = fn.toString(); | ||
fn.name = str.substring(9, str.indexOf('(')); | ||
} | ||
return fn.name; | ||
} | ||
/** | ||
* Returns true if value is not undefined or null. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
static isDefAndNotNull(val) { | ||
return core.isDef(val) && !core.isNull(val); | ||
/** | ||
* Gets an unique id. If `opt_object` argument is passed, the object is | ||
* mutated with an unique id. Consecutive calls with the same object | ||
* reference won't mutate the object again, instead the current object uid | ||
* returns. See {@link UID_PROPERTY}. | ||
* @param {Object=} opt_object Optional object to be mutated with the uid. If | ||
* not specified this method only returns the uid. | ||
* @param {boolean=} opt_noInheritance Optional flag indicating if this | ||
* object's uid property can be inherited from parents or not. | ||
* @throws {Error} when invoked to indicate the method should be overridden. | ||
*/ | ||
export function getUid(opt_object, opt_noInheritance) { | ||
if (opt_object) { | ||
var id = opt_object[UID_PROPERTY]; | ||
if (opt_noInheritance && !opt_object.hasOwnProperty(UID_PROPERTY)) { | ||
id = null; | ||
} | ||
return id || (opt_object[UID_PROPERTY] = uniqueIdCounter_++); | ||
} | ||
return uniqueIdCounter_++; | ||
} | ||
/** | ||
* Returns true if value is a document. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
static isDocument(val) { | ||
return val && typeof val === 'object' && val.nodeType === 9; | ||
} | ||
/** | ||
* The identity function. Returns its first argument. | ||
* @param {*=} opt_returnValue The single value that will be returned. | ||
* @return {?} The first argument. | ||
*/ | ||
export function identityFunction(opt_returnValue) { | ||
return opt_returnValue; | ||
} | ||
/** | ||
* Returns true if value is a dom element. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
static isElement(val) { | ||
return val && typeof val === 'object' && val.nodeType === 1; | ||
} | ||
/** | ||
* Returns true if the specified value is a boolean. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is boolean. | ||
*/ | ||
export function isBoolean(val) { | ||
return typeof val === 'boolean'; | ||
} | ||
/** | ||
* Returns true if the specified value is a function. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is a function. | ||
*/ | ||
static isFunction(val) { | ||
return typeof val === 'function'; | ||
} | ||
/** | ||
* Returns true if the specified value is not undefined. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is defined. | ||
*/ | ||
export function isDef(val) { | ||
return val !== undefined; | ||
} | ||
/** | ||
* Returns true if value is null. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
static isNull(val) { | ||
return val === null; | ||
} | ||
/** | ||
* Returns true if value is not undefined or null. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
export function isDefAndNotNull(val) { | ||
return isDef(val) && !isNull(val); | ||
} | ||
/** | ||
* Returns true if the specified value is a number. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is a number. | ||
*/ | ||
static isNumber(val) { | ||
return typeof val === 'number'; | ||
} | ||
/** | ||
* Returns true if value is a document. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
export function isDocument(val) { | ||
return val && typeof val === 'object' && val.nodeType === 9; | ||
} | ||
/** | ||
* Returns true if value is a window. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
static isWindow(val) { | ||
return val !== null && val === val.window; | ||
} | ||
/** | ||
* Returns true if value is a dom element. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
export function isElement(val) { | ||
return val && typeof val === 'object' && val.nodeType === 1; | ||
} | ||
/** | ||
* Returns true if the specified value is an object. This includes arrays | ||
* and functions. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is an object. | ||
*/ | ||
static isObject(val) { | ||
var type = typeof val; | ||
return type === 'object' && val !== null || type === 'function'; | ||
} | ||
/** | ||
* Returns true if the specified value is a function. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is a function. | ||
*/ | ||
export function isFunction(val) { | ||
return typeof val === 'function'; | ||
} | ||
/** | ||
* Returns true if value is a Promise. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
static isPromise(val) { | ||
return val && typeof val === 'object' && typeof val.then === 'function'; | ||
} | ||
/** | ||
* Returns true if value is null. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
export function isNull(val) { | ||
return val === null; | ||
} | ||
/** | ||
* Returns true if value is a string. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
static isString(val) { | ||
return typeof val === 'string' || val instanceof String; | ||
} | ||
/** | ||
* Returns true if the specified value is a number. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is a number. | ||
*/ | ||
export function isNumber(val) { | ||
return typeof val === 'number'; | ||
} | ||
/** | ||
* Merges the values of a static property a class with the values of that | ||
* property for all its super classes, and stores it as a new static | ||
* property of that class. If the static property already existed, it won't | ||
* be recalculated. | ||
* @param {!function()} constructor Class constructor. | ||
* @param {string} propertyName Property name to be collected. | ||
* @param {function(*, *):*=} opt_mergeFn Function that receives an array filled | ||
* with the values of the property for the current class and all its super classes. | ||
* Should return the merged value to be stored on the current class. | ||
* @return {boolean} Returns true if merge happens, false otherwise. | ||
*/ | ||
static mergeSuperClassesProperty(constructor, propertyName, opt_mergeFn) { | ||
var mergedName = propertyName + '_MERGED'; | ||
if (constructor.hasOwnProperty(mergedName)) { | ||
return false; | ||
} | ||
/** | ||
* Returns true if value is a window. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
export function isWindow(val) { | ||
return val !== null && val === val.window; | ||
} | ||
var merged = core.collectSuperClassesProperty(constructor, propertyName); | ||
if (opt_mergeFn) { | ||
merged = opt_mergeFn(merged); | ||
} | ||
constructor[mergedName] = merged; | ||
return true; | ||
} | ||
/** | ||
* Returns true if the specified value is an object. This includes arrays | ||
* and functions. | ||
* @param {?} val Variable to test. | ||
* @return {boolean} Whether variable is an object. | ||
*/ | ||
export function isObject(val) { | ||
var type = typeof val; | ||
return type === 'object' && val !== null || type === 'function'; | ||
} | ||
/** | ||
* Null function used for default values of callbacks, etc. | ||
* @return {void} Nothing. | ||
*/ | ||
static nullFunction() {} | ||
/** | ||
* Returns true if value is a Promise. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
export function isPromise(val) { | ||
return val && typeof val === 'object' && typeof val.then === 'function'; | ||
} | ||
/** | ||
* Unique id property prefix. | ||
* @type {String} | ||
* @protected | ||
* Returns true if value is a string. | ||
* @param {*} val | ||
* @return {boolean} | ||
*/ | ||
core.UID_PROPERTY = 'core_' + ((Math.random() * 1e9) >>> 0); | ||
export function isString(val) { | ||
return typeof val === 'string' || val instanceof String; | ||
} | ||
/** | ||
* Counter for unique id. | ||
* @type {Number} | ||
* @private | ||
* Merges the values of a export function property a class with the values of that | ||
* property for all its super classes, and stores it as a new static | ||
* property of that class. If the export function property already existed, it won't | ||
* be recalculated. | ||
* @param {!function()} constructor Class constructor. | ||
* @param {string} propertyName Property name to be collected. | ||
* @param {function(*, *):*=} opt_mergeFn Function that receives an array filled | ||
* with the values of the property for the current class and all its super classes. | ||
* Should return the merged value to be stored on the current class. | ||
* @return {boolean} Returns true if merge happens, false otherwise. | ||
*/ | ||
core.uniqueIdCounter_ = 1; | ||
export function mergeSuperClassesProperty(constructor, propertyName, opt_mergeFn) { | ||
var mergedName = propertyName + '_MERGED'; | ||
if (constructor.hasOwnProperty(mergedName)) { | ||
return false; | ||
} | ||
export default core; | ||
var merged = collectSuperClassesProperty(constructor, propertyName); | ||
if (opt_mergeFn) { | ||
merged = opt_mergeFn(merged); | ||
} | ||
constructor[mergedName] = merged; | ||
return true; | ||
} | ||
/** | ||
* Null function used for default values of callbacks, etc. | ||
* @return {void} Nothing. | ||
*/ | ||
export function nullFunction() {} |
'use strict'; | ||
import core from './core'; | ||
import * as core from './core'; | ||
import array from './array/array'; | ||
@@ -10,3 +10,10 @@ import async from './async/async'; | ||
export * from './core'; | ||
export { array, async, Disposable, object, string }; | ||
// This is for backwards compatibility, making sure that old imports for the | ||
// "core" object still work. It's best to use the named exports for each | ||
// function instead though, since that allows bundlers like Rollup to reduce the | ||
// bundle size by removing unused code. | ||
export default core; | ||
export { core, array, async, Disposable, object, string }; | ||
export { core }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57003
1637