From e52abdfada9911a2468df25148dc3be74424e3cb Mon Sep 17 00:00:00 2001 From: Chris Reeves Date: Thu, 31 Jan 2019 16:49:55 -0500 Subject: [PATCH] minifiable undefineds --- src/Ractive/prototype/observe/Array.js | 6 +++--- src/Ractive/prototype/shared/makeArrayMethod.js | 4 ++-- src/model/ComputationChild.js | 3 ++- src/model/LinkModel.js | 5 +++-- src/model/Model.js | 6 +++--- src/polyfills/array.find.js | 4 ++-- src/shared/getNewIndices.js | 4 ++-- src/shared/set.js | 4 ++-- src/utils/array.js | 4 ++-- src/utils/is.js | 4 ++++ src/view/RepeatedFragment.js | 4 ++-- src/view/items/Await.js | 4 ++-- src/view/items/Section.js | 4 ++-- src/view/items/component/Mapping.js | 4 ++-- src/view/items/element/Attribute.js | 4 ++-- src/view/items/element/Transition.js | 4 ++-- src/view/items/element/attribute/getUpdateDelegate.js | 4 ++-- src/view/items/element/binding/Binding.js | 5 +++-- src/view/items/element/binding/MultipleSelectBinding.js | 5 +++-- src/view/items/element/specials/Option.js | 6 +++--- 20 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/Ractive/prototype/observe/Array.js b/src/Ractive/prototype/observe/Array.js index f2965bfa04..65e035efab 100755 --- a/src/Ractive/prototype/observe/Array.js +++ b/src/Ractive/prototype/observe/Array.js @@ -1,5 +1,5 @@ import { removeFromArray } from 'utils/array'; -import { isArray } from 'utils/is'; +import { isArray, isUndefined } from 'utils/is'; import runloop from 'src/global/runloop'; function negativeOne() { @@ -61,7 +61,7 @@ export default class ArrayObserver { newIndices.forEach((newIndex, oldIndex) => { hadIndex[newIndex] = true; - if (newIndex !== oldIndex && start === undefined) { + if (newIndex !== oldIndex && isUndefined(start)) { start = oldIndex; } @@ -70,7 +70,7 @@ export default class ArrayObserver { } }); - if (start === undefined) start = newIndices.length; + if (isUndefined(start)) start = newIndices.length; const len = newValue.length; for (let i = 0; i < len; i += 1) { diff --git a/src/Ractive/prototype/shared/makeArrayMethod.js b/src/Ractive/prototype/shared/makeArrayMethod.js index 51445553e5..1c5f58b2ea 100755 --- a/src/Ractive/prototype/shared/makeArrayMethod.js +++ b/src/Ractive/prototype/shared/makeArrayMethod.js @@ -1,7 +1,7 @@ import { splitKeypath } from 'shared/keypaths'; import runloop from 'src/global/runloop'; import getNewIndices from 'shared/getNewIndices'; -import { isArray } from 'utils/is'; +import { isArray, isUndefined } from 'utils/is'; const arrayProto = Array.prototype; @@ -14,7 +14,7 @@ export default function(methodName) { let array = mdl.get(); if (!isArray(array)) { - if (array === undefined) { + if (isUndefined(array)) { array = []; const result = arrayProto[methodName].apply(array, args); const promise = runloop.start().then(() => result); diff --git a/src/model/ComputationChild.js b/src/model/ComputationChild.js index fbef37ef45..02f11caae1 100755 --- a/src/model/ComputationChild.js +++ b/src/model/ComputationChild.js @@ -2,6 +2,7 @@ import { capture } from 'src/global/capture'; import Model from './Model'; import { handleChange, mark, marked } from 'shared/methodCallers'; import { hasOwn } from 'utils/object'; +import { isUndefined } from 'utils/is'; export default class ComputationChild extends Model { constructor(parent, key) { @@ -64,7 +65,7 @@ export default class ComputationChild extends Model { } joinKey(key) { - if (key === undefined || key === '') return this; + if (isUndefined(key) || key === '') return this; if (!hasOwn(this.childByKey, key)) { const child = new ComputationChild(this, key); diff --git a/src/model/LinkModel.js b/src/model/LinkModel.js index 526cd40afc..7b521fbc58 100755 --- a/src/model/LinkModel.js +++ b/src/model/LinkModel.js @@ -5,6 +5,7 @@ import { rebindMatch } from 'shared/rebind'; import resolveReference from 'src/view/resolvers/resolveReference'; import noop from 'utils/noop'; import { hasOwn } from 'utils/object'; +import { isUndefined } from 'utils/is'; // temporary placeholder target for detached implicit links export const Missing = { @@ -35,7 +36,7 @@ export default class LinkModel extends ModelBase { this.owner = owner; this.target = target; - this.key = key === undefined ? owner.key : key; + this.key = isUndefined(key) ? owner.key : key; if (owner && owner.isLink) this.sourcePath = `${owner.sourcePath}.${this.key}`; if (target) target.registerLink(this); @@ -100,7 +101,7 @@ export default class LinkModel extends ModelBase { joinKey(key) { // TODO: handle nested links - if (key === undefined || key === '') return this; + if (isUndefined(key) || key === '') return this; if (!hasOwn(this.childByKey, key)) { const child = new LinkModel(this, this, this.target.joinKey(key), key); diff --git a/src/model/Model.js b/src/model/Model.js index 8d1648310b..c1b50d80ba 100755 --- a/src/model/Model.js +++ b/src/model/Model.js @@ -2,7 +2,7 @@ import ModelBase, { checkDataLink, maybeBind, shuffle } from './ModelBase'; import LinkModel from './LinkModel'; // eslint-disable-line no-unused-vars import getComputationSignature from 'src/Ractive/helpers/getComputationSignature'; import { capture } from 'src/global/capture'; -import { isArray, isEqual, isNumeric, isObjectLike } from 'utils/is'; +import { isArray, isEqual, isNumeric, isObjectLike, isUndefined } from 'utils/is'; import { handleChange, mark, markForce, marked, teardown } from 'shared/methodCallers'; import Ticker from 'shared/Ticker'; import getPrefixer from './helpers/getPrefixer'; @@ -211,11 +211,11 @@ export default class Model extends ModelBase { joinKey(key, opts) { if (this._link) { - if (opts && opts.lastLink !== false && (key === undefined || key === '')) return this; + if (opts && opts.lastLink !== false && (isUndefined(key) || key === '')) return this; return this._link.joinKey(key); } - if (key === undefined || key === '') return this; + if (isUndefined(key) || key === '') return this; let child; if (hasOwn(this.childByKey, key)) child = this.childByKey[key]; diff --git a/src/polyfills/array.find.js b/src/polyfills/array.find.js index 5e910b9e01..119abbe9a3 100644 --- a/src/polyfills/array.find.js +++ b/src/polyfills/array.find.js @@ -1,11 +1,11 @@ import { hasOwn, defineProperty } from 'utils/object'; -import { isFunction } from 'utils/is'; +import { isFunction, isUndefined } from 'utils/is'; /* istanbul ignore if */ if (!Array.prototype.find) { defineProperty(Array.prototype, 'find', { value(callback, thisArg) { - if (this === null || this === undefined) + if (this === null || isUndefined(this)) throw new TypeError('Array.prototype.find called on null or undefined'); if (!isFunction(callback)) throw new TypeError(`${callback} is not a function`); diff --git a/src/shared/getNewIndices.js b/src/shared/getNewIndices.js index 2b7d66e8e3..fc298d6576 100644 --- a/src/shared/getNewIndices.js +++ b/src/shared/getNewIndices.js @@ -1,4 +1,4 @@ -import { isNumber } from 'utils/is'; +import { isNumber, isUndefined } from 'utils/is'; // This function takes an array, the name of a mutator method, and the // arguments to call that mutator method with, and returns an array that @@ -72,7 +72,7 @@ function getSpliceEquivalent(length, methodName, args) { args[0] = length + Math.max(args[0], -length); } - if (args[0] === undefined) args[0] = 0; + if (isUndefined(args[0])) args[0] = 0; while (args.length < 2) { args.push(length - args[0]); diff --git a/src/shared/set.js b/src/shared/set.js index 3412892516..7037778259 100755 --- a/src/shared/set.js +++ b/src/shared/set.js @@ -1,4 +1,4 @@ -import { isArray, isObject, isObjectType, isFunction, isString } from 'utils/is'; +import { isArray, isObject, isObjectType, isFunction, isString, isUndefined } from 'utils/is'; import { warnIfDebug } from 'utils/log'; import resolveReference from 'src/view/resolvers/resolveReference'; import runloop from '../global/runloop'; @@ -35,7 +35,7 @@ export function set(pairs, options) { if (!array) array = target; // if there's not an array there yet, go ahead and set - if (target === undefined) { + if (isUndefined(target)) { model.set(array); } else { if (!isArray(target) || !isArray(array)) { diff --git a/src/utils/array.js b/src/utils/array.js index eb821f44e8..8462863c9b 100644 --- a/src/utils/array.js +++ b/src/utils/array.js @@ -1,4 +1,4 @@ -import { isArray, isString } from './is'; +import { isArray, isString, isUndefined } from './is'; export function addToArray(array, value) { const index = array.indexOf(value); @@ -44,7 +44,7 @@ export function ensureArray(x) { return [x]; } - if (x === undefined) { + if (isUndefined(x)) { return []; } diff --git a/src/utils/is.js b/src/utils/is.js index fd69f48836..e6f6828cc6 100644 --- a/src/utils/is.js +++ b/src/utils/is.js @@ -47,3 +47,7 @@ export function isString(thing) { export function isNumber(thing) { return typeof thing === 'number'; } + +export function isUndefined(thing) { + return thing === undefined; +} diff --git a/src/view/RepeatedFragment.js b/src/view/RepeatedFragment.js index 777e03edc4..0b502d281a 100755 --- a/src/view/RepeatedFragment.js +++ b/src/view/RepeatedFragment.js @@ -1,5 +1,5 @@ import { createDocumentFragment } from 'utils/dom'; -import { isArray, isObject, isObjectType } from 'utils/is'; +import { isArray, isObject, isObjectType, isUndefined } from 'utils/is'; import { findMap, buildNewIndices } from 'utils/array'; import { toEscapedString, toString, shuffled, update } from 'shared/methodCallers'; import Fragment, { getKeypath } from './Fragment'; @@ -443,7 +443,7 @@ export default class RepeatedFragment { idx = pos = 0; while (idx < len) { // if there's not an existing thing to shuffle, handle that - if (map[idx] === undefined) { + if (isUndefined(map[idx])) { next = iters[idx] = this.createIteration(idx, idx); if (parentNode) { anchor = prev[pos]; diff --git a/src/view/items/Await.js b/src/view/items/Await.js index acc74cf919..17c45db9f8 100644 --- a/src/view/items/Await.js +++ b/src/view/items/Await.js @@ -1,7 +1,7 @@ import { ATTRIBUTE, CATCH, ELEMENT, ELSE, INTERPOLATOR, SECTION, THEN } from 'src/config/types'; import Partial from './Partial'; import { assign } from 'utils/object'; -import { isFunction } from 'utils/is'; +import { isFunction, isUndefined } from 'utils/is'; function extract(tpl, type, name) { const p = tpl.f.find(s => s.t === type); @@ -55,7 +55,7 @@ export default function Await(options) { handle.setTemplate(error); } ); - } else if (attrs.for === undefined) { + } else if (isUndefined(attrs.for)) { handle.setTemplate(undef); } else { handle.set('@local.value', attrs.for); diff --git a/src/view/items/Section.js b/src/view/items/Section.js index 1ef76b09cd..9bddd30160 100755 --- a/src/view/items/Section.js +++ b/src/view/items/Section.js @@ -6,7 +6,7 @@ import { SECTION_WITH } from 'config/types'; import { createDocumentFragment } from 'utils/dom'; -import { isArray, isObject, isObjectLike } from 'utils/is'; +import { isArray, isObject, isObjectLike, isUndefined } from 'utils/is'; import { keep } from 'shared/set'; import runloop from 'src/global/runloop'; import Fragment from '../Fragment'; @@ -26,7 +26,7 @@ function isEmpty(value) { function getType(value, hasIndexRef) { if (hasIndexRef || isArray(value)) return SECTION_EACH; if (isObjectLike(value)) return SECTION_IF_WITH; - if (value === undefined) return null; + if (isUndefined(value)) return null; return SECTION_IF; } diff --git a/src/view/items/component/Mapping.js b/src/view/items/component/Mapping.js index 9b871cd626..6ac408a54f 100644 --- a/src/view/items/component/Mapping.js +++ b/src/view/items/component/Mapping.js @@ -7,7 +7,7 @@ import resolve from '../../resolvers/resolve'; import runloop from '../../../global/runloop'; import { warnIfDebug } from 'utils/log'; import { splitKeypath } from 'shared/keypaths'; -import { isArray, isObjectType, isString } from 'utils/is'; +import { isArray, isObjectType, isString, isUndefined } from 'utils/is'; export default class Mapping extends Item { constructor(options) { @@ -88,7 +88,7 @@ function createMapping(item) { }); // initialize parent side of the mapping from child data - if (val === undefined && !model.isReadonly && item.name in childData) { + if (isUndefined(val) && !model.isReadonly && item.name in childData) { model.set(childData[item.name]); } } else if (!isObjectType(val) || template[0].x) { diff --git a/src/view/items/element/Attribute.js b/src/view/items/element/Attribute.js index 23658e597e..b898c60404 100755 --- a/src/view/items/element/Attribute.js +++ b/src/view/items/element/Attribute.js @@ -9,7 +9,7 @@ import findElement from '../shared/findElement'; import getUpdateDelegate from './attribute/getUpdateDelegate'; import propertyNames from './attribute/propertyNames'; import { inAttributes } from './ConditionalAttribute'; -import { isArray, isString } from 'utils/is'; +import { isArray, isString, isUndefined } from 'utils/is'; function lookupNamespace(node, prefix) { const qualified = `xmlns:${prefix}`; @@ -50,7 +50,7 @@ export default class Attribute extends Item { this.value = options.template.f; if (this.value === 0) { this.value = ''; - } else if (this.value === undefined) { + } else if (isUndefined(this.value)) { this.value = true; } return; diff --git a/src/view/items/element/Transition.js b/src/view/items/element/Transition.js index a33bfa79a5..1b7c5858ab 100755 --- a/src/view/items/element/Transition.js +++ b/src/view/items/element/Transition.js @@ -1,6 +1,6 @@ import { win } from 'config/environment'; import { addToArray, removeFromArray } from 'utils/array'; -import { isArray, isObject, isFunction, isNumber, isString } from 'utils/is'; +import { isArray, isObject, isFunction, isNumber, isString, isUndefined } from 'utils/is'; import noop from 'utils/noop'; import { warnOnceIfDebug } from 'utils/log'; import { missingPlugin } from 'config/errors'; @@ -320,7 +320,7 @@ function nearestProp(prop, ractive, rendering) { while (instance) { if ( hasOwn(instance, prop) && - (rendering === undefined || rendering ? instance.rendering : instance.unrendering) + (isUndefined(rendering) || rendering ? instance.rendering : instance.unrendering) ) return instance[prop]; instance = instance.component && instance.component.ractive; diff --git a/src/view/items/element/attribute/getUpdateDelegate.js b/src/view/items/element/attribute/getUpdateDelegate.js index 115d237fbe..e16c8052e2 100755 --- a/src/view/items/element/attribute/getUpdateDelegate.js +++ b/src/view/items/element/attribute/getUpdateDelegate.js @@ -5,7 +5,7 @@ import noop from 'utils/noop'; import hyphenateCamel from 'utils/hyphenateCamel'; import { readStyle, readClass } from 'src/view/helpers/specialAttrs'; import { keys as objectKeys } from 'utils/object'; -import { isArray, isString } from 'utils/is'; +import { isArray, isString, isUndefined } from 'utils/is'; const textTypes = [ undefined, @@ -140,7 +140,7 @@ function updateContentEditableValue(reset) { if (!this.locked) { if (reset) this.node.innerHTML = ''; - else this.node.innerHTML = value === undefined ? '' : value; + else this.node.innerHTML = isUndefined(value) ? '' : value; } } diff --git a/src/view/items/element/binding/Binding.js b/src/view/items/element/binding/Binding.js index 407b3640fd..f879e83a81 100755 --- a/src/view/items/element/binding/Binding.js +++ b/src/view/items/element/binding/Binding.js @@ -2,6 +2,7 @@ import runloop from 'src/global/runloop'; import { warnOnceIfDebug } from 'utils/log'; import noop from 'utils/noop'; import findElement from '../../shared/findElement'; +import { isUndefined } from 'utils/is'; export default class Binding { constructor(element, name = 'value') { @@ -32,9 +33,9 @@ export default class Binding { // initialise value, if it's undefined let value = model.get(); - this.wasUndefined = value === undefined; + this.wasUndefined = isUndefined(value); - if (value === undefined && this.getInitialValue) { + if (isUndefined(value) && this.getInitialValue) { value = this.getInitialValue(); model.set(value); } diff --git a/src/view/items/element/binding/MultipleSelectBinding.js b/src/view/items/element/binding/MultipleSelectBinding.js index cc2193cc82..a30df6e64c 100755 --- a/src/view/items/element/binding/MultipleSelectBinding.js +++ b/src/view/items/element/binding/MultipleSelectBinding.js @@ -2,6 +2,7 @@ import { arrayContentsMatch } from 'utils/array'; import getSelectedOptions from 'utils/getSelectedOptions'; import Binding from './Binding'; import handleDomEvent from './handleDomEvent'; +import { isUndefined } from 'utils/is'; export default class MultipleSelectBinding extends Binding { getInitialValue() { @@ -34,7 +35,7 @@ export default class MultipleSelectBinding extends Binding { const value = this.getValue(); - if (previousValue === undefined || !arrayContentsMatch(value, previousValue)) { + if (isUndefined(previousValue) || !arrayContentsMatch(value, previousValue)) { super.handleChange(); } @@ -46,7 +47,7 @@ export default class MultipleSelectBinding extends Binding { this.element.on('change', handleDomEvent); - if (this.model.get() === undefined) { + if (isUndefined(this.model.get())) { // get value from DOM, if possible this.handleChange(); } diff --git a/src/view/items/element/specials/Option.js b/src/view/items/element/specials/Option.js index 05a9d83528..c0a543b6f4 100755 --- a/src/view/items/element/specials/Option.js +++ b/src/view/items/element/specials/Option.js @@ -1,7 +1,7 @@ import { removeFromArray } from 'utils/array'; import Element from '../../Element'; import findElement from '../../shared/findElement'; -import { isArray } from 'utils/is'; +import { isArray, isUndefined } from 'utils/is'; export default class Option extends Element { constructor(options) { @@ -10,7 +10,7 @@ export default class Option extends Element { // If the value attribute is missing, use the element's content, // as long as it isn't disabled - if (template.a.value === undefined && !('disabled' in template.a)) { + if (isUndefined(template.a.value) && !('disabled' in template.a)) { template.a.value = template.f || ''; } @@ -59,7 +59,7 @@ export default class Option extends Element { isSelected() { const optionValue = this.getAttribute('value'); - if (optionValue === undefined || !this.select) { + if (isUndefined(optionValue) || !this.select) { return false; }