From d003ec60c781ec5af1f618c983e7ffe465ac2e05 Mon Sep 17 00:00:00 2001 From: Aleck Greenham Date: Sun, 15 Oct 2017 18:29:01 +0100 Subject: [PATCH] Use lodash utils instead of including them in the package --- package.json | 8 ++- src/shortcut-manager.js | 14 +++-- src/utils.js | 49 --------------- test/utils.js | 132 ---------------------------------------- 4 files changed, 17 insertions(+), 186 deletions(-) delete mode 100644 src/utils.js delete mode 100644 test/utils.js diff --git a/package.json b/package.json index 1963005..e3811bb 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,13 @@ "combokeys": "^3.0.0", "events": "^1.0.2", "invariant": "^2.1.0", - "just-reduce-object": "^1.0.3", + "lodash.compact": "^3.0.1", + "lodash.findkey": "^4.6.0", + "lodash.flatten": "^4.4.0", + "lodash.isarray": "^4.0.0", + "lodash.isplainobject": "^4.0.6", + "lodash.map": "^4.6.0", + "lodash.reduce": "^4.6.0", "platform": "^1.3.0", "prop-types": "^15.5.8" }, diff --git a/src/shortcut-manager.js b/src/shortcut-manager.js index d2a2e86..f339472 100644 --- a/src/shortcut-manager.js +++ b/src/shortcut-manager.js @@ -1,9 +1,15 @@ -import reduce from 'just-reduce-object' +import reduce from 'lodash.reduce' import invariant from 'invariant' +import isPlainObject from 'lodash.isplainobject' +import findKey from 'lodash.findkey' +import isArray from 'lodash.isarray' +import map from 'lodash.map' +import compact from 'lodash.compact' +import flatten from 'lodash.flatten' + import { EventEmitter } from 'events' -import helpers from './helpers' -import { isPlainObject, findKey, isArray, map, compact, flatten } from './utils' +import helpers from './helpers' const warning = (text) => { if (process && process.env.NODE_ENV !== 'production') { @@ -58,7 +64,7 @@ class ShortcutManager extends EventEmitter { getAllShortcutsForPlatform(platformName) { const _transformShortcuts = (shortcuts) => { - return reduce(shortcuts, (result, keyName, keyValue) => { + return reduce(shortcuts, (result, keyValue, keyName) => { if (isPlainObject(keyValue)) { if (keyValue[platformName]) { keyValue = keyValue[platformName] diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index c078ab4..0000000 --- a/src/utils.js +++ /dev/null @@ -1,49 +0,0 @@ -export const isArray = arr => Array.isArray(arr) - -export const isPlainObject = (obj) => { - const isObject = typeof obj === 'object' && obj !== null && !isArray(obj) - if (!isObject || (obj.toString && obj.toString() !== '[object Object]')) return false - const proto = Object.getPrototypeOf(obj) - if (proto === null) { - return true - } - const Ctor = Object.prototype.hasOwnProperty.call(proto, 'constructor') && proto.constructor - return typeof Ctor === 'function' && Ctor instanceof Ctor && - Function.prototype.toString.call(Ctor) === Function.prototype.toString.call(Object) -} - -export const findKey = (obj, fn) => { - if (!isPlainObject(obj) && !isArray(obj)) return - - const keys = Object.keys(obj) - return keys.find(key => fn(obj[key])) -} - -export const compact = arr => arr.filter(Boolean) - -const flattenOnce = (arr, recurse = true) => { - return arr.reduce((acc, val) => { - if (isArray(val) && recurse) return acc.concat(flattenOnce(val, false)) - acc.push(val) - return acc - }, []) -} - -export const flatten = (arr) => { - if (!isArray(arr)) throw new Error('flatten expects an array') - return flattenOnce(arr) -} - -export const map = (itr, fn) => { - if (isArray(itr)) return itr.map(fn) - - const results = [] - const keys = Object.keys(itr) - const len = keys.length - for (let i = 0; i < len; i += 1) { - const key = keys[i] - results.push(fn(itr[key], key)) - } - - return results -} diff --git a/test/utils.js b/test/utils.js deleted file mode 100644 index 160873e..0000000 --- a/test/utils.js +++ /dev/null @@ -1,132 +0,0 @@ -import chai from 'chai' -import _ from 'lodash' -import { isArray, isPlainObject, findKey, compact, flatten, map } from '../src/utils' - -describe('utils', () => { - const { expect } = chai - let primitives - - beforeEach(() => { - function fn() { this.a = 1 } - - primitives = [ - ['array'], - { object: true }, - Object.create(null), - 'string', - null, - undefined, - NaN, - new Map([[ 1, 'one' ], [ 2, 'two' ]]), - new fn(), - true, - 42, - ] - }) - - describe('isArray', () => { - it('should be true for arrays', () => { - primitives.forEach((val, idx) => { - if (idx === 0) { - expect(isArray(val)).to.be.true - expect(_.isArray(val)).to.be.true - } else { - expect(isArray(val)).to.be.false - expect(_.isArray(val)).to.be.false - } - }) - }) - }) - - describe('isPlainObject', () => { - it('should be true for plain objects', () => { - primitives.forEach((val, idx) => { - if (idx === 1 || idx === 2) { - expect(isPlainObject(val)).to.be.true - expect(_.isPlainObject(val)).to.be.true - } else { - expect(isPlainObject(val)).to.be.false - expect(_.isPlainObject(val)).to.be.false - } - }) - }) - }) - - describe('findKey', () => { - it('should return the matching key', () => { - const obj = { - simple: 1, - obj: { - val: 4, - }, - } - - const checkOne = val => val === 1 - const checkTwo = val => typeof val === 'object' - - expect(findKey(obj, checkOne)).to.deep.equal(_.findKey(obj, checkOne)) - expect(findKey(obj, checkTwo)).to.deep.equal(_.findKey(obj, checkTwo)) - }) - }) - - describe('compact', () => { - it('removes falsy values', () => { - const values = [ - true, - false, - 10, - 0, - null, - undefined, - NaN, - '', - 'false, null, 0, "", undefined, and NaN are falsy', - ] - - expect(compact(values)).to.deep.equal(_.compact(values)) - }) - }) - - describe('flatten', () => { - it('flattens an array 1 level', () => { - const value = [1, [2, [3, [4]], 5, [[[6], 7], 8], 9]] - expect(flatten(value)).to.deep.equal(_.flatten(value)) - }) - }) - - describe('map', () => { - it('should map an array', () => { - const values = [1, 2, 3, 4] - const mapFn = val => val * 10 - - expect(map(values, mapFn)).to.deep.equal(_.map(values, mapFn)) - expect(map(values, mapFn)).to.deep.equal([10, 20, 30, 40]) - - // ensure that values array is not mutated - expect(values).to.deep.equal([1, 2, 3, 4]) - }) - - it('should map an object', () => { - const obj = { - one: 1, - two: 2, - three: 3, - } - const mapFn = (val, key) => `${key} - ${val * 10}` - - expect(map(obj, mapFn)).to.deep.equal(_.map(obj, mapFn)) - expect(map(obj, mapFn)).to.deep.equal([ - 'one - 10', - 'two - 20', - 'three - 30', - ]) - - // ensure the object was not mutated - expect(obj).to.deep.equal({ - one: 1, - two: 2, - three: 3, - }) - }) - }) -})