-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathwrapper.js
More file actions
158 lines (147 loc) · 3.68 KB
/
wrapper.js
File metadata and controls
158 lines (147 loc) · 3.68 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
147
148
149
150
151
152
153
154
155
156
157
158
import functions from 'functions';
/**
* The chain wrapper constructor.
*
* @ignore
* @param {string} subject The string to be wrapped.
* @param {boolean} [explicitChain=false] A boolean that indicates if the chain sequence is explicit or implicit.
* @return {ChainWrapper} Returns a new instance of `ChainWrapper`
* @constructor
*/
function ChainWrapper(subject, explicitChain) {
this._wrappedValue = subject;
this._explicitChain = explicitChain;
}
/**
* Unwraps the chain sequence wrapped value.
*
* @memberof Chain
* @since 1.0.0
* @function __proto__value
* @return {*} Returns the unwrapped value.
* @example
* v
* .chain('Hello world')
* .replace('Hello', 'Hi')
* .lowerCase()
* .slugify()
* .value()
* // => 'hi-world'
*
* v(' Space travel ')
* .trim()
* .truncate(8)
* .value()
* // => 'Space...'
*/
ChainWrapper.prototype.value = function() {
return this._wrappedValue;
};
/**
* Override the default object valueOf().
*
* @ignore
* @return {*} Returns the wrapped value.
*/
ChainWrapper.prototype.valueOf = function() {
return this.value();
};
/**
* Returns the wrapped value to be used in JSON.stringify().
*
* @ignore
* @return {*} Returns the wrapped value.
*/
ChainWrapper.prototype.toJSON = function() {
return this.value();
};
/**
* Returns the string representation of the wrapped value.
*
* @ignore
* @return {string} Returns the string representation.
*/
ChainWrapper.prototype.toString = function() {
return String(this.value());
};
/**
* Creates a new chain object that enables <i>explicit</i> chain sequences.
* Use `v.prototype.value()` to unwrap the result. <br/>
* Does not modify the wrapped value.
*
* @memberof Chain
* @since 1.0.0
* @function __proto__chain
* @return {Object} Returns the wrapper in <i>explicit</i> mode.
* @example
* v('Back to School')
* .chain()
* .lowerCase()
* .words()
* .value()
* // => ['back', 'to', 'school']
*
* v(" Back to School ")
* .chain()
* .trim()
* .truncate(7)
* .value()
* // => 'Back...'
*/
ChainWrapper.prototype.chain = function() {
return new ChainWrapper(this._wrappedValue, true);
};
/**
* Modifies the wrapped value with the invocation result of `changer` function. The current wrapped value is the
* argument of `changer` invocation.
*
* @memberof Chain
* @since 1.0.0
* @function __proto__thru
* @param {Function} changer The function to invoke.
* @return {Object} Returns the new wrapper that wraps the invocation result of `changer`.
* @example
* v
* .chain('sun is shining')
* .words()
* .thru(function(words) {
* return words[0];
* })
* .value()
* // => 'sun'
*
*/
ChainWrapper.prototype.thru = function(changer) {
if (typeof changer === 'function') {
return new ChainWrapper(changer(this._wrappedValue), this._explicitChain);
}
return this;
};
/**
* A boolean that indicates if the chain sequence is explicit or implicit.
* @ignore
* @type {boolean}
* @private
*/
ChainWrapper.prototype._explicitChain = true;
/**
* Make a voca function chainable.
*
* @ignore
* @param {Function} functionInstance The function to make chainable
* @return {Function} Returns the chainable function
*/
function makeFunctionChainable(functionInstance) {
return function(...args) {
const result = functionInstance(this._wrappedValue, ...args);
if (this._explicitChain || typeof result === 'string') {
return new ChainWrapper(result, this._explicitChain);
} else {
return result;
}
};
}
Object.keys(functions).forEach(function(name) {
ChainWrapper.prototype[name] = makeFunctionChainable(functions[name]);
});
export default ChainWrapper;