-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathbind.js
More file actions
146 lines (122 loc) · 3.9 KB
/
bind.js
File metadata and controls
146 lines (122 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Copyright (c) 2018-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import util from 'util';
import chalk from 'chalk';
import pretty from 'pretty-format';
type Table = Array<Array<any>>;
type PrettyArgs = {
args: Array<mixed>,
title: string,
};
const EXPECTED_COLOR = chalk.green;
const RECEIVED_COLOR = chalk.red;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const PRETTY_PLACEHOLDER = '%p';
const INDEX_PLACEHOLDER = '%#';
export default (cb: Function) => (...args: any) =>
function eachBind(title: string, test: Function): void {
if (args.length === 1) {
const table: Table = args[0].every(Array.isArray)
? args[0]
: args[0].map(entry => [entry]);
return table.forEach((row, i) =>
cb(arrayFormat(title, i, ...row), applyRestParams(row, test)),
);
}
const templateStrings = args[0];
const data = args.slice(1);
const keys = getHeadingKeys(templateStrings[0]);
const table = buildTable(data, keys.length, keys);
const missingData = data.length % keys.length;
if (missingData > 0) {
const error = new Error(
'Not enough arguments supplied for given headings:\n' +
EXPECTED_COLOR(keys.join(' | ')) +
'\n\n' +
'Received:\n' +
RECEIVED_COLOR(pretty(data)) +
'\n\n' +
`Missing ${RECEIVED_COLOR(missingData.toString())} ${pluralize(
'argument',
missingData,
)}`,
);
if (Error.captureStackTrace) {
Error.captureStackTrace(error, eachBind);
}
return cb(title, () => {
throw error;
});
}
return table.forEach(row =>
cb(interpolate(title, row), applyObjectParams(row, test)),
);
};
const getPrettyIndexes = placeholders =>
placeholders.reduce(
(indexes, placeholder, index) =>
placeholder === PRETTY_PLACEHOLDER ? indexes.concat(index) : indexes,
[],
);
const arrayFormat = (title, rowIndex, ...args) => {
const placeholders = title.match(SUPPORTED_PLACEHOLDERS) || [];
const prettyIndexes = getPrettyIndexes(placeholders);
const {title: prettyTitle, args: remainingArgs} = args.reduce(
(acc: PrettyArgs, arg, index) => {
if (prettyIndexes.indexOf(index) !== -1) {
return {
args: acc.args,
title: acc.title
.replace(PRETTY_PLACEHOLDER, pretty(arg, {maxDepth: 1, min: true}))
.replace(INDEX_PLACEHOLDER, `${rowIndex}`),
};
}
return {
args: acc.args.concat([arg]),
title: acc.title.replace(INDEX_PLACEHOLDER, `${rowIndex}`),
};
},
{args: [], title},
);
return util.format(
prettyTitle,
...remainingArgs.slice(0, placeholders.length - prettyIndexes.length),
);
};
const applyRestParams = (params: Array<any>, test: Function) => {
if (params.length < test.length) return done => test(...params, done);
return () => test(...params);
};
const getHeadingKeys = (headings: string): Array<string> =>
headings.replace(/\s/g, '').split('|');
const buildTable = (
data: Array<any>,
rowSize: number,
keys: Array<string>,
): Array<any> =>
Array.from({length: data.length / rowSize})
.map((_, index) => data.slice(index * rowSize, index * rowSize + rowSize))
.map(row =>
row.reduce(
(acc, value, index) => Object.assign({}, acc, {[keys[index]]: value}),
{},
),
);
const interpolate = (title: string, data: any) =>
Object.keys(data).reduce(
(acc, key) =>
acc.replace('$' + key, pretty(data[key], {maxDepth: 1, min: true})),
title,
);
const applyObjectParams = (obj: any, test: Function) => {
if (test.length > 1) return done => test(obj, done);
return () => test(obj);
};
const pluralize = (word: string, count: number) =>
word + (count === 1 ? '' : 's');