Skip to content

Commit

Permalink
Add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
guigrpa committed Oct 25, 2017
1 parent 2d7ae1d commit aaf912a
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 143 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rules:
- error
- allowForLoopAfterthoughts: true
no-continue: off
arrow-parens: off
key-spacing:
- warn
-
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"lodash": "4.17.4",
"nyc": "8.3.1",
"oao": "^0.10.4",
"prettier": "^1.7.4",
"seamless-immutable": "7.1.2",
"uglifyjs": "2.4.10",
"xxl": "1.2.0"
Expand Down
124 changes: 78 additions & 46 deletions src/timm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const INVALID_ARGS = 'INVALID_ARGS';
// ===============================================
// ### Helpers
// ===============================================
type ArrayOrObject = Array<any>|Object;
type Key = number|string;
type ArrayOrObject = Array<any> | Object;
type Key = number | string;

function throwStr(msg: string) {
throw new Error(msg);
Expand All @@ -34,10 +34,19 @@ export function clone<T: ArrayOrObject>(obj: T): T {
return out;
}

function doMerge(fAddDefaults: boolean, fDeep: boolean, first: ArrayOrObject, ...rest: any): any {
function doMerge(
fAddDefaults: boolean,
fDeep: boolean,
first: ArrayOrObject,
...rest: any
): any {
let out = first;
!(out != null) && throwStr(process.env.NODE_ENV !== 'production' ?
'At least one object should be provided to merge()' : INVALID_ARGS);
!(out != null) &&
throwStr(
process.env.NODE_ENV !== 'production'
? 'At least one object should be provided to merge()'
: INVALID_ARGS
);
let fChanged = false;
for (let idx = 0; idx < rest.length; idx++) {
const obj = rest[idx];
Expand All @@ -64,7 +73,7 @@ function doMerge(fAddDefaults: boolean, fDeep: boolean, first: ArrayOrObject, ..

function isObject(o: any): boolean {
const type = typeof o;
return (o != null) && (type === 'object' || type === 'function');
return o != null && (type === 'object' || type === 'function');
}

// _deepFreeze = (obj) ->
Expand Down Expand Up @@ -95,7 +104,7 @@ function isObject(o: any): boolean {
// -- ```
// `array.concat(val)` also handles the scalar case,
// but is apparently very slow
export function addLast<T>(array: Array<T>, val: Array<T>|T): Array<T> {
export function addLast<T>(array: Array<T>, val: Array<T> | T): Array<T> {
if (Array.isArray(val)) return array.concat(val);
return array.concat([val]);
}
Expand All @@ -114,7 +123,7 @@ export function addLast<T>(array: Array<T>, val: Array<T>|T): Array<T> {
// -- arr3 = addFirst(arr, ['c', 'd'])
// -- // ['c', 'd', 'a', 'b']
// -- ```
export function addFirst<T>(array: Array<T>, val: Array<T>|T): Array<T> {
export function addFirst<T>(array: Array<T>, val: Array<T> | T): Array<T> {
if (Array.isArray(val)) return val.concat(array);
return [val].concat(array);
}
Expand Down Expand Up @@ -178,7 +187,11 @@ export function removeFirst<T>(array: Array<T>): Array<T> {
// -- insert(arr, 1, ['d', 'e'])
// -- // ['a', 'd', 'e', 'b', 'c']
// -- ```
export function insert<T>(array: Array<T>, idx: number, val: Array<T>|T): Array<T> {
export function insert<T>(
array: Array<T>,
idx: number,
val: Array<T> | T
): Array<T> {
return array
.slice(0, idx)
.concat(Array.isArray(val) ? val : [val])
Expand All @@ -204,9 +217,7 @@ export function insert<T>(array: Array<T>, idx: number, val: Array<T>|T): Array<
// -- ```
export function removeAt<T>(array: Array<T>, idx: number): Array<T> {
if (idx >= array.length || idx < 0) return array;
return array
.slice(0, idx)
.concat(array.slice(idx + 1));
return array.slice(0, idx).concat(array.slice(idx + 1));
}

// -- #### replaceAt()
Expand All @@ -228,7 +239,11 @@ export function removeAt<T>(array: Array<T>, idx: number): Array<T> {
// -- replaceAt(arr, 1, 'b') === arr
// -- // true
// -- ```
export function replaceAt<T>(array: Array<T>, idx: number, newItem: T): Array<T> {
export function replaceAt<T>(
array: Array<T>,
idx: number,
newItem: T
): Array<T> {
if (array[idx] === newItem) return array;
const len = array.length;
const result = Array(len);
Expand Down Expand Up @@ -262,12 +277,13 @@ export function replaceAt<T>(array: Array<T>, idx: number, newItem: T): Array<T>
// -- getIn(obj, ['e', 1])
// -- // 'b'
// -- ```
export function getIn(
obj: ?ArrayOrObject,
path: Array<Key>
): any {
!(Array.isArray(path)) && throwStr(process.env.NODE_ENV !== 'production' ?
'A path array should be provided when calling getIn()' : INVALID_ARGS);
export function getIn(obj: ?ArrayOrObject, path: Array<Key>): any {
!Array.isArray(path) &&
throwStr(
process.env.NODE_ENV !== 'production'
? 'A path array should be provided when calling getIn()'
: INVALID_ARGS
);
if (obj == null) return undefined;
let ptr: any = obj;
for (let i = 0; i < path.length; i++) {
Expand Down Expand Up @@ -353,7 +369,9 @@ function doSetIn<T: ArrayOrObject>(
if (idx === path.length - 1) {
newValue = val;
} else {
const nestedObj = isObject(obj) ? obj[key] : typeof path[idx + 1] === 'number' ? [] : {};
const nestedObj = isObject(obj)
? obj[key]
: typeof path[idx + 1] === 'number' ? [] : {};
newValue = doSetIn(nestedObj, path, val, idx + 1);
}
return set(obj, key, newValue);
Expand Down Expand Up @@ -459,16 +477,18 @@ export function updateIn<T: ArrayOrObject>(
// -- ```
export function merge(
a: Object,
b: ?Object, c: ?Object,
d: ?Object, e: ?Object,
f: ?Object, ...rest: Array<?Object>
b: ?Object,
c: ?Object,
d: ?Object,
e: ?Object,
f: ?Object,
...rest: Array<?Object>
): Object {
return rest.length ?
doMerge.call(null, false, false, a, b, c, d, e, f, ...rest) :
doMerge(false, false, a, b, c, d, e, f);
return rest.length
? doMerge.call(null, false, false, a, b, c, d, e, f, ...rest)
: doMerge(false, false, a, b, c, d, e, f);
}


// -- #### mergeDeep()
// -- Returns a new object built as follows: the overlapping keys from the
// -- second one overwrite the corresponding entries from the first one.
Expand Down Expand Up @@ -501,13 +521,18 @@ export function merge(
// -- mergeDeep(obj1, { c: { a: 1 } }) === obj1
// -- // true
// -- ```
export function mergeDeep(a: Object,
b: ?Object, c: ?Object,
d: ?Object, e: ?Object,
f: ?Object, ...rest: Array<?Object>): Object {
return rest.length ?
doMerge.call(null, false, true, a, b, c, d, e, f, ...rest) :
doMerge(false, true, a, b, c, d, e, f);
export function mergeDeep(
a: Object,
b: ?Object,
c: ?Object,
d: ?Object,
e: ?Object,
f: ?Object,
...rest: Array<?Object>
): Object {
return rest.length
? doMerge.call(null, false, true, a, b, c, d, e, f, ...rest)
: doMerge(false, true, a, b, c, d, e, f);
}

// -- #### mergeIn()
Expand All @@ -533,10 +558,14 @@ export function mergeDeep(a: Object,
// -- // true
// -- ```
export function mergeIn<T: ArrayOrObject>(
a: T, path: Array<Key>,
b: ?Object, c: ?Object,
d: ?Object, e: ?Object,
f: ?Object, ...rest: Array<?Object>
a: T,
path: Array<Key>,
b: ?Object,
c: ?Object,
d: ?Object,
e: ?Object,
f: ?Object,
...rest: Array<?Object>
): T {
let prevVal = getIn(a, path);
if (prevVal == null) prevVal = {};
Expand Down Expand Up @@ -565,7 +594,7 @@ export function mergeIn<T: ArrayOrObject>(
// -- omit(obj, 'z') === obj1
// -- // true
// -- ```
export function omit(obj: Object, attrs: Array<string>|string): Object {
export function omit(obj: Object, attrs: Array<string> | string): Object {
const omitList = Array.isArray(attrs) ? attrs : [attrs];
let fDoSomething = false;
for (let i = 0; i < omitList.length; i++) {
Expand Down Expand Up @@ -609,13 +638,16 @@ export function omit(obj: Object, attrs: Array<string>|string): Object {
// -- ```
export function addDefaults(
a: Object,
b: ?Object, c: ?Object,
d: ?Object, e: ?Object,
f: ?Object, ...rest: Array<?Object>
b: ?Object,
c: ?Object,
d: ?Object,
e: ?Object,
f: ?Object,
...rest: Array<?Object>
): Object {
return rest.length ?
doMerge.call(null, true, false, a, b, c, d, e, f, ...rest) :
doMerge(true, false, a, b, c, d, e, f);
return rest.length
? doMerge.call(null, true, false, a, b, c, d, e, f, ...rest)
: doMerge(true, false, a, b, c, d, e, f);
}

// ===============================================
Expand All @@ -633,7 +665,7 @@ const timm = {

getIn,
// eslint-disable-next-line object-shorthand
set: set, // so that flow doesn't complain
set: set, // so that flow doesn't complain
setIn,
update,
updateIn,
Expand Down
30 changes: 15 additions & 15 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ if (process.env.TEST_MINIFIED_LIB) {
}

const ARR0 = ['a', 'b', 'c'];
const ARR = ['a', 'b', 'c'];
const ARR = ['a', 'b', 'c'];

//------------------------------------------------
// addLast()
//------------------------------------------------
test('addLast: single value', (t) => {
test('addLast: single value', t => {
const arr2 = timm.addLast(ARR, 'd');
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
t.deepEqual(arr2, ['a', 'b', 'c', 'd']);
});

test('addLast: multiple values', (t) => {
test('addLast: multiple values', t => {
const arr2 = timm.addLast(ARR, ['d', 'e']);
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
Expand All @@ -32,14 +32,14 @@ test('addLast: multiple values', (t) => {
//------------------------------------------------
// addFirst()
//------------------------------------------------
test('addFirst: single value', (t) => {
test('addFirst: single value', t => {
const arr2 = timm.addFirst(ARR, 'd');
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
t.deepEqual(arr2, ['d', 'a', 'b', 'c']);
});

test('addFirst: multiple values', (t) => {
test('addFirst: multiple values', t => {
const arr2 = timm.addFirst(ARR, ['d', 'e']);
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
Expand All @@ -49,14 +49,14 @@ test('addFirst: multiple values', (t) => {
//------------------------------------------------
// removeLast()
//------------------------------------------------
test('removeLast: changing', (t) => {
test('removeLast: changing', t => {
const arr2 = timm.removeLast(ARR);
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
t.deepEqual(arr2, ['a', 'b']);
});

test('removeLast: should return the same array when it hasn\'t changed', (t) => {
test("removeLast: should return the same array when it hasn't changed", t => {
const arr = [];
const arr2 = timm.removeLast(arr);
t.deepEqual(arr2, arr);
Expand All @@ -65,14 +65,14 @@ test('removeLast: should return the same array when it hasn\'t changed', (t) =>
//------------------------------------------------
// removeLast()
//------------------------------------------------
test('removeFirst: changing', (t) => {
test('removeFirst: changing', t => {
const arr2 = timm.removeFirst(ARR);
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
t.deepEqual(arr2, ['b', 'c']);
});

test('removeFirst: should return the same array when it hasn\'t changed', (t) => {
test("removeFirst: should return the same array when it hasn't changed", t => {
const arr = [];
const arr2 = timm.removeFirst(arr);
t.deepEqual(arr2, arr);
Expand All @@ -81,14 +81,14 @@ test('removeFirst: should return the same array when it hasn\'t changed', (t) =>
//------------------------------------------------
// insert()
//------------------------------------------------
test('insert: single value', (t) => {
test('insert: single value', t => {
const arr2 = timm.insert(ARR, 1, 'e');
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
t.deepEqual(arr2, ['a', 'e', 'b', 'c']);
});

test('insert: multiple values', (t) => {
test('insert: multiple values', t => {
const arr2 = timm.insert(ARR, 1, ['e', 'f']);
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
Expand All @@ -98,14 +98,14 @@ test('insert: multiple values', (t) => {
//------------------------------------------------
// removeAt()
//------------------------------------------------
test('removeAt', (t) => {
test('removeAt', t => {
const arr2 = timm.removeAt(ARR, 1);
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
t.deepEqual(arr2, ['a', 'c']);
});

test('removeAt', (t) => {
test('removeAt', t => {
const arr2 = timm.removeAt(ARR, 5);
t.deepEqual(ARR, ARR0);
t.is(arr2, ARR);
Expand All @@ -114,14 +114,14 @@ test('removeAt', (t) => {
//------------------------------------------------
// replaceAt()
//------------------------------------------------
test('replaceAt: changing', (t) => {
test('replaceAt: changing', t => {
const arr2 = timm.replaceAt(ARR, 1, 'd');
t.deepEqual(ARR, ARR0);
t.not(arr2, ARR);
t.deepEqual(arr2, ['a', 'd', 'c']);
});

test('replaceAt: should return the same object when it hasn\'t changed', (t) => {
test("replaceAt: should return the same object when it hasn't changed", t => {
const arr2 = timm.replaceAt(ARR, 1, 'b');
t.is(arr2, ARR);
});
2 changes: 1 addition & 1 deletion test/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ if (process.env.TEST_MINIFIED_LIB) {
timm = require('../lib/timm');
}

test('sanity', (t) => {
test('sanity', t => {
t.truthy(timm.set);
});
Loading

0 comments on commit aaf912a

Please sign in to comment.