Skip to content

Commit

Permalink
minifiable undefineds
Browse files Browse the repository at this point in the history
  • Loading branch information
evs-chris committed Jan 31, 2019
1 parent 4719cef commit e52abdf
Show file tree
Hide file tree
Showing 20 changed files with 48 additions and 40 deletions.
6 changes: 3 additions & 3 deletions src/Ractive/prototype/observe/Array.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/Ractive/prototype/shared/makeArrayMethod.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/model/ComputationChild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/model/LinkModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/polyfills/array.find.js
Original file line number Diff line number Diff line change
@@ -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`);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/getNewIndices.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/set.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/array.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -44,7 +44,7 @@ export function ensureArray(x) {
return [x];
}

if (x === undefined) {
if (isUndefined(x)) {
return [];
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ export function isString(thing) {
export function isNumber(thing) {
return typeof thing === 'number';
}

export function isUndefined(thing) {
return thing === undefined;
}
4 changes: 2 additions & 2 deletions src/view/RepeatedFragment.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions src/view/items/Await.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/view/items/Section.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/view/items/component/Mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/view/items/element/Attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/view/items/element/Transition.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/view/items/element/attribute/getUpdateDelegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/view/items/element/binding/Binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/view/items/element/binding/MultipleSelectBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions src/view/items/element/specials/Option.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 || '';
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit e52abdf

Please sign in to comment.