From dd9dd6114b2555b9b613c9310c0a751cced588b9 Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Mon, 15 Jun 2015 01:41:34 -0400 Subject: [PATCH] [CLEANUP Beta] isArray Prefer Array.isArray for ember-metal variant, but not runtime variant MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (╯°□°)╯︵ ʎɐɹɹɐsı¬ --- .../ember-htmlbars/lib/helpers/-bind-attr-class.js | 3 +-- packages/ember-metal/lib/get_properties.js | 3 +-- packages/ember-metal/lib/mixin.js | 5 ++--- packages/ember-metal/lib/utils.js | 13 ++----------- packages/ember-metal/lib/watch_key.js | 5 ++--- packages/ember-metal/lib/watch_path.js | 5 ++--- packages/ember-metal/lib/watching.js | 8 ++------ packages/ember-routing/lib/system/route.js | 5 ++--- .../ember-runtime/lib/computed/reduce_computed.js | 5 ++--- .../lib/computed/reduce_computed_macros.js | 3 +-- packages/ember-runtime/lib/copy.js | 3 +-- packages/ember-runtime/lib/main.js | 6 +++--- packages/ember-runtime/lib/mixins/mutable_array.js | 4 ++-- packages/ember-runtime/lib/utils.js | 3 +-- .../ember-views/lib/mixins/class_names_support.js | 7 ++----- 15 files changed, 26 insertions(+), 52 deletions(-) diff --git a/packages/ember-htmlbars/lib/helpers/-bind-attr-class.js b/packages/ember-htmlbars/lib/helpers/-bind-attr-class.js index 651b0e4a158..6a5121371b7 100644 --- a/packages/ember-htmlbars/lib/helpers/-bind-attr-class.js +++ b/packages/ember-htmlbars/lib/helpers/-bind-attr-class.js @@ -4,12 +4,11 @@ */ import { get } from 'ember-metal/property_get'; -import { isArray } from 'ember-metal/utils'; export default function bindAttrClassHelper(params) { var value = params[0]; - if (isArray(value)) { + if (Array.isArray(value)) { value = get(value, 'length') !== 0; } diff --git a/packages/ember-metal/lib/get_properties.js b/packages/ember-metal/lib/get_properties.js index 80031fd6a2a..414482b8d46 100644 --- a/packages/ember-metal/lib/get_properties.js +++ b/packages/ember-metal/lib/get_properties.js @@ -1,5 +1,4 @@ import { get } from 'ember-metal/property_get'; -import { isArray } from 'ember-metal/utils'; /** To get multiple properties at once, call `Ember.getProperties` @@ -29,7 +28,7 @@ export default function getProperties(obj) { var propertyNames = arguments; var i = 1; - if (arguments.length === 2 && isArray(arguments[1])) { + if (arguments.length === 2 && Array.isArray(arguments[1])) { i = 0; propertyNames = arguments[1]; } diff --git a/packages/ember-metal/lib/mixin.js b/packages/ember-metal/lib/mixin.js index 10bddf91ec2..655d44c70f8 100644 --- a/packages/ember-metal/lib/mixin.js +++ b/packages/ember-metal/lib/mixin.js @@ -16,8 +16,7 @@ import { guidFor, meta as metaFor, wrap, - makeArray, - isArray + makeArray } from 'ember-metal/utils'; import expandProperties from 'ember-metal/expand_properties'; import { @@ -221,7 +220,7 @@ function applyConcatenatedProperties(obj, key, value, values) { function applyMergedProperties(obj, key, value, values) { var baseValue = values[key] || obj[key]; - Ember.assert(`You passed in \`${JSON.stringify(value)}\` as the value for \`${key}\` but \`${key}\` cannot be an Array`, !isArray(value)); + Ember.assert(`You passed in \`${JSON.stringify(value)}\` as the value for \`${key}\` but \`${key}\` cannot be an Array`, !Array.isArray(value)); if (!baseValue) { return value; } diff --git a/packages/ember-metal/lib/utils.js b/packages/ember-metal/lib/utils.js index 387f36aa0cc..97af9560aca 100644 --- a/packages/ember-metal/lib/utils.js +++ b/packages/ember-metal/lib/utils.js @@ -719,14 +719,6 @@ var deprecatedTryCatchFinally = function() { var toString = Object.prototype.toString; -var isArray = Array.isArray || function(value) { - return value !== null && - value !== undefined && - typeof value === 'object' && - typeof value.length === 'number' && - toString.call(value) === '[object Array]'; -}; - /** Forces the passed object to be part of an array. If the object is already an array, it will return the object. Otherwise, it will add the object to @@ -752,7 +744,7 @@ var isArray = Array.isArray || function(value) { */ export function makeArray(obj) { if (obj === null || obj === undefined) { return []; } - return isArray(obj) ? obj : [obj]; + return Array.isArray(obj) ? obj : [obj]; } /** @@ -776,7 +768,7 @@ export function inspect(obj) { if (obj === undefined) { return 'undefined'; } - if (isArray(obj)) { + if (Array.isArray(obj)) { return '[' + obj + ']'; } // for non objects @@ -853,7 +845,6 @@ export { META_DESC, EMPTY_META, meta, - isArray, makeArray, tryCatchFinally, deprecatedTryCatchFinally, diff --git a/packages/ember-metal/lib/watch_key.js b/packages/ember-metal/lib/watch_key.js index f41f8fc1b4f..473b49989a8 100644 --- a/packages/ember-metal/lib/watch_key.js +++ b/packages/ember-metal/lib/watch_key.js @@ -1,7 +1,6 @@ import isEnabled from 'ember-metal/features'; import { - meta as metaFor, - isArray + meta as metaFor } from 'ember-metal/utils'; import { MANDATORY_SETTER_FUNCTION, @@ -10,7 +9,7 @@ import { export function watchKey(obj, keyName, meta) { // can't watch length on Array - it is special... - if (keyName === 'length' && isArray(obj)) { return; } + if (keyName === 'length' && Array.isArray(obj)) { return; } var m = meta || metaFor(obj); var watching = m.watching; diff --git a/packages/ember-metal/lib/watch_path.js b/packages/ember-metal/lib/watch_path.js index cb2e623314d..a5d67dc4f10 100644 --- a/packages/ember-metal/lib/watch_path.js +++ b/packages/ember-metal/lib/watch_path.js @@ -1,6 +1,5 @@ import { - meta as metaFor, - isArray + meta as metaFor } from 'ember-metal/utils'; import { ChainNode } from 'ember-metal/chains'; @@ -20,7 +19,7 @@ function chainsFor(obj, meta) { export function watchPath(obj, keyPath, meta) { // can't watch length on Array - it is special... - if (keyPath === 'length' && isArray(obj)) { return; } + if (keyPath === 'length' && Array.isArray(obj)) { return; } var m = meta || metaFor(obj); var watching = m.watching; diff --git a/packages/ember-metal/lib/watching.js b/packages/ember-metal/lib/watching.js index 86cc22ac835..8c36c569629 100644 --- a/packages/ember-metal/lib/watching.js +++ b/packages/ember-metal/lib/watching.js @@ -2,9 +2,6 @@ @module ember-metal */ -import { - isArray -} from 'ember-metal/utils'; import { removeChainWatcher, flushPendingChains @@ -17,7 +14,6 @@ import { watchPath, unwatchPath } from 'ember-metal/watch_path'; - import { isPath } from 'ember-metal/path_cache'; @@ -37,7 +33,7 @@ import { */ function watch(obj, _keyPath, m) { // can't watch length on Array - it is special... - if (_keyPath === 'length' && isArray(obj)) { return; } + if (_keyPath === 'length' && Array.isArray(obj)) { return; } if (!isPath(_keyPath)) { watchKey(obj, _keyPath, m); @@ -57,7 +53,7 @@ watch.flushPending = flushPendingChains; export function unwatch(obj, _keyPath, m) { // can't watch length on Array - it is special... - if (_keyPath === 'length' && isArray(obj)) { return; } + if (_keyPath === 'length' && Array.isArray(obj)) { return; } if (!isPath(_keyPath)) { unwatchKey(obj, _keyPath, m); diff --git a/packages/ember-routing/lib/system/route.js b/packages/ember-routing/lib/system/route.js index 5683d06f10e..5f93bff5ab0 100644 --- a/packages/ember-routing/lib/system/route.js +++ b/packages/ember-routing/lib/system/route.js @@ -8,7 +8,6 @@ import isNone from 'ember-metal/is_none'; import { computed } from 'ember-metal/computed'; import merge from 'ember-metal/merge'; import { - isArray, typeOf } from 'ember-runtime/utils'; import run from 'ember-metal/run_loop'; @@ -172,7 +171,7 @@ var Route = EmberObject.extend(ActionHandler, Evented, { var urlKey = desc.as || this.serializeQueryParamKey(propName); var defaultValue = get(controllerProto, propName); - if (isArray(defaultValue)) { + if (Array.isArray(defaultValue)) { defaultValue = Ember.A(defaultValue.slice()); } @@ -2198,7 +2197,7 @@ function getQueryParamsFor(route, state) { } function copyDefaultValue(value) { - if (isArray(value)) { + if (Array.isArray(value)) { return Ember.A(value.slice()); } return value; diff --git a/packages/ember-runtime/lib/computed/reduce_computed.js b/packages/ember-runtime/lib/computed/reduce_computed.js index 527cbcd5e10..a84adb1530e 100644 --- a/packages/ember-runtime/lib/computed/reduce_computed.js +++ b/packages/ember-runtime/lib/computed/reduce_computed.js @@ -2,8 +2,7 @@ import Ember from 'ember-metal/core'; // Ember.assert import { get as e_get } from 'ember-metal/property_get'; import { guidFor, - meta as metaFor, - isArray + meta as metaFor } from 'ember-metal/utils'; import EmberError from 'ember-metal/error'; import { @@ -520,7 +519,7 @@ function ReduceComputedProperty(options) { Ember.assert( 'dependent array ' + dependentKey + ' must be an `Ember.Array`. ' + 'If you are not extending arrays, you will need to wrap native arrays with `Ember.A`', - !(isArray(get(this, dependentKey)) && !EmberArray.detect(get(this, dependentKey)))); + !(Array.isArray(get(this, dependentKey)) && !EmberArray.detect(get(this, dependentKey)))); if (!partiallyRecomputeFor(this, dependentKey)) { return; } diff --git a/packages/ember-runtime/lib/computed/reduce_computed_macros.js b/packages/ember-runtime/lib/computed/reduce_computed_macros.js index 1e86e2991cf..0cd149833d9 100644 --- a/packages/ember-runtime/lib/computed/reduce_computed_macros.js +++ b/packages/ember-runtime/lib/computed/reduce_computed_macros.js @@ -6,7 +6,6 @@ import Ember from 'ember-metal/core'; // Ember.assert import { get } from 'ember-metal/property_get'; import { - isArray, guidFor } from 'ember-metal/utils'; import EmberError from 'ember-metal/error'; @@ -772,7 +771,7 @@ function propertySort(itemsKey, sortPropertiesKey) { var sortProperty, idx, asc; Ember.assert('Cannot sort: \'' + sortPropertiesKey + '\' is not an array.', - isArray(sortPropertyDefinitions)); + Array.isArray(sortPropertyDefinitions)); changeMeta.property.clearItemPropertyKeys(itemsKey); diff --git a/packages/ember-runtime/lib/copy.js b/packages/ember-runtime/lib/copy.js index f3440fe48a4..1a5ca10aeaf 100644 --- a/packages/ember-runtime/lib/copy.js +++ b/packages/ember-runtime/lib/copy.js @@ -1,5 +1,4 @@ import Ember from 'ember-metal/core'; -import { isArray } from 'ember-metal/utils'; import EmberObject from 'ember-runtime/system/object'; import Copyable from 'ember-runtime/mixins/copyable'; @@ -21,7 +20,7 @@ function _copy(obj, deep, seen, copies) { // IMPORTANT: this specific test will detect a native array only. Any other // object will need to implement Copyable. - if (isArray(obj)) { + if (Array.isArray(obj)) { ret = obj.slice(); if (deep) { diff --git a/packages/ember-runtime/lib/main.js b/packages/ember-runtime/lib/main.js index e5abc613785..3e985803be5 100644 --- a/packages/ember-runtime/lib/main.js +++ b/packages/ember-runtime/lib/main.js @@ -82,7 +82,7 @@ import RSVP from 'ember-runtime/ext/rsvp'; // just for side effect of extend import 'ember-runtime/ext/string'; // just for side effect of extending String.prototype import 'ember-runtime/ext/function'; // just for side effect of extending Function.prototype -import { typeOf, isArray } from 'ember-runtime/utils'; +import { typeOf } from 'ember-runtime/utils'; // END IMPORTS // BEGIN EXPORTS @@ -117,8 +117,8 @@ Ember.ArrayComputedProperty = ArrayComputedProperty; Ember.reduceComputed = reduceComputed; Ember.ReduceComputedProperty = ReduceComputedProperty; -Ember.typeOf = typeOf; -Ember.isArray = isArray; +Ember.typeOf = typeOf; +Ember.isArray = Array.isArray; // ES6TODO: this seems a less than ideal way/place to add properties to Ember.computed var EmComputed = Ember.computed; diff --git a/packages/ember-runtime/lib/mixins/mutable_array.js b/packages/ember-runtime/lib/mixins/mutable_array.js index 46f5ef1a2ee..3b707d65e14 100644 --- a/packages/ember-runtime/lib/mixins/mutable_array.js +++ b/packages/ember-runtime/lib/mixins/mutable_array.js @@ -19,12 +19,12 @@ var EMPTY = []; // import { get } from 'ember-metal/property_get'; -import { isArray } from 'ember-metal/utils'; import EmberError from 'ember-metal/error'; import { Mixin } from 'ember-metal/mixin'; import EmberArray from 'ember-runtime/mixins/array'; import MutableEnumerable from 'ember-runtime/mixins/mutable_enumerable'; import Enumerable from 'ember-runtime/mixins/enumerable'; + /** This mixin defines the API for modifying array-like objects. These methods can be applied only to a collection that keeps its items in an ordered set. @@ -194,7 +194,7 @@ export default Mixin.create(EmberArray, MutableEnumerable, { @public */ pushObjects(objects) { - if (!(Enumerable.detect(objects) || isArray(objects))) { + if (!(Enumerable.detect(objects) || Array.isArray(objects))) { throw new TypeError('Must pass Ember.Enumerable to Ember.MutableArray#pushObjects'); } this.replace(get(this, 'length'), 0, objects); diff --git a/packages/ember-runtime/lib/utils.js b/packages/ember-runtime/lib/utils.js index 5326c0a2301..6d76d02f362 100644 --- a/packages/ember-runtime/lib/utils.js +++ b/packages/ember-runtime/lib/utils.js @@ -1,6 +1,5 @@ import EmberArray from 'ember-runtime/mixins/array'; import EmberObject from 'ember-runtime/system/object'; -import {isArray as _isArray} from 'ember-metal/utils'; // ........................................ // TYPING & ARRAY MESSAGING @@ -44,7 +43,7 @@ var toString = Object.prototype.toString; */ export function isArray(obj) { if (!obj || obj.setInterval) { return false; } - if (_isArray(obj)) { return true; } + if (Array.isArray(obj)) { return true; } if (EmberArray.detect(obj)) { return true; } let type = typeOf(obj); diff --git a/packages/ember-views/lib/mixins/class_names_support.js b/packages/ember-views/lib/mixins/class_names_support.js index ae9bd600c76..6a4cbd26ca2 100644 --- a/packages/ember-views/lib/mixins/class_names_support.js +++ b/packages/ember-views/lib/mixins/class_names_support.js @@ -5,9 +5,6 @@ import Ember from 'ember-metal/core'; import { Mixin } from 'ember-metal/mixin'; import { A as emberA } from 'ember-runtime/system/native_array'; -import { - isArray -} from 'ember-metal/utils'; var EMPTY_ARRAY = []; @@ -22,10 +19,10 @@ export default Mixin.create({ init() { this._super(...arguments); - Ember.assert('Only arrays are allowed for \'classNameBindings\'', isArray(this.classNameBindings)); + Ember.assert(`Only arrays are allowed for 'classNameBindings'`, Array.isArray(this.classNameBindings)); this.classNameBindings = emberA(this.classNameBindings.slice()); - Ember.assert('Only arrays of static class strings are allowed for \'classNames\'. For dynamic classes, use \'classNameBindings\'.', isArray(this.classNames)); + Ember.assert(`Only arrays of static class strings are allowed for 'classNames'. For dynamic classes, use 'classNameBindings'.`, Array.isArray(this.classNames)); this.classNames = emberA(this.classNames.slice()); },