Skip to content

Commit e622b02

Browse files
author
Jocelyn Badgley (Twipped)
committed
Split everything out into separate modules.
Added promise functions: pdelay pdefer pall ptry pmap prace preduce
1 parent 029fa98 commit e622b02

18 files changed

+1586
-1390
lines changed

rollup.config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const bannerConfig = {
1111
export default [
1212

1313
{
14-
input: 'src/utils.js',
14+
input: 'src/index.js',
1515
output: {
1616
file: 'dist/utils.cjs.js',
1717
format: 'cjs',
@@ -24,7 +24,7 @@ export default [
2424
},
2525

2626
{
27-
input: 'src/utils.js',
27+
input: 'src/index.js',
2828
output: {
2929
file: 'dist/utils.esm.js',
3030
format: 'esm',
@@ -35,7 +35,7 @@ export default [
3535
],
3636
},
3737
{
38-
input: 'src/utils.js',
38+
input: 'src/index.js',
3939
output: {
4040
file: 'dist/utils.esm.min.js',
4141
format: 'esm',
@@ -57,7 +57,7 @@ export default [
5757
},
5858

5959
{
60-
input: 'src/utils.js',
60+
input: 'src/index.js',
6161
output: {
6262
file: 'dist/utils.browser.js',
6363
format: 'umd',

src/any.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
import { truthy } from './isType';
3+
import { flatten } from './collections';
4+
5+
export function all (...args) {
6+
let input;
7+
if (args.length > 1) {
8+
input = args;
9+
} else {
10+
input = flatten(args[0], 1);
11+
}
12+
13+
let result = input.shift();
14+
for (const value of input) {
15+
if (!truthy(result)) {
16+
return false;
17+
}
18+
result = value;
19+
}
20+
21+
return result;
22+
}
23+
24+
export function any (...args) {
25+
let input;
26+
if (args.length > 1) {
27+
input = args;
28+
} else {
29+
input = flatten(args[0], 1);
30+
}
31+
32+
for (const value of input) {
33+
if (truthy(value)) {
34+
return value;
35+
}
36+
}
37+
38+
return input[input.length - 1];
39+
}
40+
41+
export function none (...args) {
42+
return !any(...args);
43+
}

src/anyBy.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
import {
3+
isFunction,
4+
isArray,
5+
isSet,
6+
isMap,
7+
isObject,
8+
} from './isType';
9+
import { iteratee } from './functions';
10+
import { entries } from './iterators';
11+
12+
export function allBy (collection, predicate = null) {
13+
if (!collection) return false;
14+
if (predicate === null) {
15+
predicate = (v) => v;
16+
} else if (!isFunction(predicate)) {
17+
predicate = iteratee(predicate);
18+
}
19+
20+
const it = entries(collection);
21+
22+
let i = 0;
23+
for (const [ k, v ] of it) {
24+
if (!predicate(v, k, i++)) return false;
25+
}
26+
27+
return true;
28+
}
29+
30+
31+
export function anyBy (collection, predicate = null) {
32+
if (!collection) return false;
33+
if (predicate === null) {
34+
predicate = (v) => v;
35+
} else if (!isFunction(iteratee)) {
36+
predicate = iteratee(predicate);
37+
}
38+
39+
if (isArray(collection)) {
40+
let i = 0;
41+
for (const value of collection) {
42+
if (predicate(value, i, i++)) return true;
43+
}
44+
return false;
45+
}
46+
47+
if (isSet(collection)) {
48+
let i = 0;
49+
for (const item of collection) {
50+
if (predicate(item, i, i++)) return true;
51+
}
52+
return false;
53+
}
54+
55+
// received a Map
56+
if (isMap(collection)) {
57+
let i = 0;
58+
for (const [ key, value ] of collection.entries()) {
59+
if (predicate(value, key, i++)) return true;
60+
}
61+
return false;
62+
}
63+
64+
// received an object hash
65+
if (isObject(collection)) {
66+
let i = 0;
67+
for (const [ key, value ] of Object.entries(collection)) {
68+
if (predicate(value, key, i++)) return true;
69+
}
70+
return false;
71+
}
72+
73+
return !!collection;
74+
}
75+
76+
77+
export function noneBy (collection, predicate = null) {
78+
return !anyBy(collection, predicate);
79+
}

src/anyOf.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
import { uc } from './text';
3+
import { isFunction } from './isType';
4+
import { anyBy, allBy } from './anyBy';
5+
import { isEqualTo } from './equality';
6+
7+
export function anyOf (...args) {
8+
args = args.flat(Infinity).map(uc);
9+
if (!anyBy(args, isFunction)) {
10+
// arguments do not contain a function, so we can optimize
11+
if (args.length === 1) return (tok) => uc(tok) === args[0];
12+
return (tok) => args.includes(uc(tok));
13+
}
14+
15+
args = args.map((a) => isFunction(a) && a || isEqualTo(a));
16+
if (args.length === 1) return (tok) => args[0](tok);
17+
return (tok) => anyBy(args, (check) => check(tok));
18+
}
19+
20+
export function allOf (...args) {
21+
args = args.flat(Infinity).map((a) => isFunction(a) && a || isEqualTo(a));
22+
if (args.length === 1) return (tok) => args[0](tok);
23+
return (tok) => allBy(args, (check) => check(tok));
24+
}
25+
26+
export function noneOf (...args) {
27+
args = args.flat(Infinity).map((a) => isFunction(a) && a || isEqualTo(a));
28+
if (args.length === 1) return (tok) => args[0](tok);
29+
return (tok) => !anyBy(args, (check) => check(tok));
30+
}

src/assert.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
import {
3+
isArray,
4+
isObject,
5+
isNumber,
6+
isString,
7+
} from './isType';
8+
9+
export function assert (ok, message) {
10+
if (!ok) throw new TypeError(message);
11+
}
12+
13+
assert.isArray = (ok, message) => assert(isArray(ok), message);
14+
assert.isObject = (ok, message) => assert(isObject(ok), message);
15+
assert.isPlainObject = (ok, message) => assert(isObject(ok, true), message);
16+
assert.isString = (ok, message) => assert(isString(ok), message);
17+
assert.isNumber = (ok, message) => assert(isNumber(ok), message);

0 commit comments

Comments
 (0)