Skip to content

Commit 07316bc

Browse files
committed
Just for the Lolz
1 parent 19401da commit 07316bc

3 files changed

Lines changed: 19 additions & 30 deletions

File tree

src/arrays.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const each = (arr, cb) => arr.forEach((v, i) => cb(v, i));
1+
const each = (arr, cb) => {
2+
for (let i = 0; i < arr.length; i++) {
3+
cb(arr[i], i);
4+
}
5+
};
26

37
const map = (arr, cb) => {
48
const output = [];
@@ -23,10 +27,7 @@ const filter = (arr, cb) => {
2327
return output;
2428
};
2529

26-
const flatten = arr => arr.reduce((a, c) => {
27-
if (Array.isArray(c)) return a.concat(flatten(c));
28-
return a.concat(c);
29-
}, []);
30+
const flatten = arr => arr.reduce((a, c) => (Array.isArray(c) ? a.concat(flatten(c)) : a.concat(c)), []);
3031

3132
/* eslint-enable no-unused-vars, max-len */
3233

src/closure.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,12 @@ const counterFactory = () => {
1313

1414
const limitFunctionCallCount = (cb, n) => {
1515
let count = 0;
16-
return (...args) => {
17-
if (count === n) return null;
18-
count++;
19-
return cb(...args);
20-
};
16+
return (...args) => (count === n ? null : ++count && cb(...args));
2117
};
2218

2319
const cacheFunction = (cb) => {
2420
const cache = {};
25-
return (arg) => {
26-
if (arg in cache) return cache[arg];
27-
cache[arg] = cb(arg);
28-
return cache[arg];
29-
};
21+
return arg => (arg in cache ? cache[arg] : cache[arg] = cb(arg));
3022
};
3123

3224
/* eslint-enable no-unused-vars */

src/objects.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,26 @@ const keys = obj => Object.keys(obj);
22

33
const values = obj => Object.values(obj);
44

5-
const mapObject = (obj, cb) => {
6-
return Object.keys(obj).reduce((a, c) => {
7-
a[c] = cb(obj[c]);
8-
return a;
9-
}, {});
10-
};
5+
const mapObject = (obj, cb) => Object.keys(obj).reduce((a, c) => {
6+
a[c] = cb(obj[c]);
7+
return a;
8+
}, {});
119

1210
const pairs = obj => Object.entries(obj);
1311

14-
const invert = (obj) => {
15-
return Object.entries(obj).reduce((a, c) => {
16-
const [k, v] = c;
17-
a[v] = k;
18-
return a;
19-
}, {});
20-
};
12+
const invert = obj => Object.entries(obj).reduce((a, c) => {
13+
const [k, v] = c;
14+
a[v] = k;
15+
return a;
16+
}, {});
17+
2118

2219
const defaults = (obj, defaultProps) => {
2320
const x = Object.entries(obj);
2421
const y = Object.entries(defaultProps);
2522
return [...x, ...y].reduce((a, c) => {
2623
const [k, v] = c;
27-
if (k in a) return a;
28-
a[k] = v;
24+
if (!(k in a)) a[k] = v;
2925
return a;
3026
}, {});
3127
};

0 commit comments

Comments
 (0)