/
index.js
52 lines (45 loc) · 1.11 KB
/
index.js
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
import { manipulations as functions } from '@laces/functions';
/**
* @typedef {object} Chain
* @property {Array<function>} wrappedFns - Stringy functions.
* @property {function} value - Function to return wrapped string
*/
/**
* Creates a fluent interface to chain stringy operations together.
* @requires module:@laces/functions
* @param {string} string - The string to chain operations from.
* @returns {Chain} Returns Chain
* @example
* chain('my string')
* .value();
* // returns 'my string'
* @example
* chain('my string')
* .toCamelCase()
* .capitalize()
* .repeat(2)
* .value();
* // returns 'MyStringMyString'
*/
const chain = function (string) {
let s = string;
const wrappedFns = Object
.keys(functions)
.reduce((wrappers, key) => {
const fn = functions[key];
function wrapped(...args) {
s = fn(s, ...args);
return this;
}
return {
...wrappers,
[key]: wrapped,
};
}, {});
return {
...wrappedFns,
// value returns the wrapped string
value: () => s,
};
};
export default chain;