Repository navigation
Expand file tree
/
Copy pathstring.js
More file actions
278 lines (265 loc) · 8.05 KB
/
Copy pathstring.js
File metadata and controls
278 lines (265 loc) · 8.05 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import {minusOneToUndefined} from './utils.js';
import {nary} from "./arity.js";
/**
* trim output is a string without white characters around it.
*
* @HindleyMilner trim :: string -> string
*
* @pure
* @param {string} string
* @return {string}
*
* @example
* import {trim} from '@7urtle/lambda';
*
* trim(' a \n '); // => 'a'
*/
export const trim = string => string.trim();
/**
* testRegEx outputs true if string b passes regular expression a.
*
* testRegEx can be called both as a curried unary function or as a standard binary function.
*
* @HindleyMilner testRegEx :: regex -> string -> boolean
*
* @pure
* @param {regex} regex
* @param {string} string
* @return {boolean}
*
* @example
* import {testRegEx} from '@7urtle/lambda';
*
* testRegEx(/[a-z]/)('7urtle'); // => true
* testRegEx(/[0-9]/)('1'); // => true
* testRegEx(/[0-9]/)('abc'); // => false
*
* // testRegEx can be called both as a curried unary function or as a standard binary function
* testRegEx(/[a-z]/)('7urtle') === testRegEx(/[a-z]/, '7urtle');
*/
export const testRegEx = nary(regex => string => regex.test(string));
/**
* substr outputs substring based on provided string, start and limit.
*
* substr can be called both as a curried unary function or as a standard ternary function.
*
* @HindleyMilner substr :: number -> number -> string -> string
*
* @pure
* @param {number} limit
* @param {number} start
* @param {string} string
* @return {string}
*
* @example
* import {substr} from '@7urtle/lambda';
*
* substr(3)(1)('7urtle'); // => 'urt'
* substr(1)(0)('7urtle'); // => '7'
* substr(1)(-1)('7urtle'); // => 'e'
*
* // substr can be called both as a curried unary function or as a standard ternary function
* substr(3)(1)('7urtle') === substr(3, 1, '7urtle');
*/
export const substr = nary(limit => start => string => string.substr(start, limit));
/**
* firstLetterOf outputs the first letter of a provided string.
*
* @HindleyMilner firstLetterOf :: string -> string
*
* @pure
* @param {string} string
* @return {string}
*
* @example
* import {firstLetterOf} from '@7urtle/lambda';
*
* firstLetterOf('7urtle'); // => '7'
*/
export const firstLetterOf = string => string.substr(0, 1);
/**
* lastLetterOf outputs the last letter of a provided string.
*
* @HindleyMilner lastLetterOf :: string -> string
*
* @pure
* @param {string} string
* @return {string}
*
* @example
* import {lastLetterOf} from '@7urtle/lambda';
*
* lastLetterOf('7urtle'); // => 'e'
*/
export const lastLetterOf = string => string.substr(-1, 1);
/**
* startsWith outputs true if an input string starts with provided string.
*
* startsWith can be called both as a curried unary function or as a standard binary function.
*
* @HindleyMilner startsWith :: string -> string -> boolean
*
* @pure
* @param {string} substring
* @param {string} string
* @return {boolean}
*
* @example
* import {startsWith} from '@7urtle/lambda';
*
* startsWith('7')('7urtle'); // => true
* startsWith('7urtl')('7urtle'); // => true
* startsWith('8urtl')('7urtle'); // => false
*
* // startsWith can be called both as a curried unary function or as a standard binary function
* startsWith('7')('7urtle') === startsWith('7', '7urtle');
*/
export const startsWith = nary(substring => string => string.startsWith(substring));
/**
* endsWith outputs true if an input string ends with provided string.
*
* endsWith can be called both as a curried unary function or as a standard binary function.
*
* @HindleyMilner endsWith :: string -> string -> boolean
*
* @pure
* @param {string} substring
* @param {string} string
* @return {boolean}
*
* @example
* import {endsWith} from '@7urtle/lambda';
*
* endsWith('7e')('7urtle'); // => true
* endsWith('urtle')('7urtle'); // => true
* endsWith('urtls')('7urtle'); // => false
*
* // endsWith can be called both as a curried unary function or as a standard binary function
* endsWith('e')('7urtle') === endsWith('e', '7urtle');
*/
export const endsWith = nary(substring => string => string.endsWith(substring));
/**
* repeat outputs new string repeating input string inputted count of times.
*
* repeat can be called both as a curried unary function or as a standard binary function.
*
* @HindleyMilner repeat :: number -> string -> string
*
* @pure
* @param {number} count
* @param {string} string
* @return {string}
*
* @example
* import {repeat} from '@7urtle/lambda';
*
* repeat(2)('7urtle'); // => '7urtle7urtle'
* repeat(0)('7urtle'); // => ''
*
* // repeat can be called both as a curried unary function or as a standard binary function
* repeat(2)('7urtle') === repeat(2, '7urtle');
*/
export const repeat = nary(count => string => string.repeat(count));
/**
* replace outputs new string replacing input substring with input replacement string in input string.
*
* replace can be called both as a curried unary function or as a standard ternary function.
*
* @HindleyMilner replace :: string -> string -> string -> string
*
* @pure
* @param {string} replacement
* @param {string} substring
* @param {string} string
* @return {string}
*
* @example
* import {replace} from '@7urtle/lambda';
*
* replace('8')('7')('7urtle'); // => '8urtle'
* replace('7')('')('7urtle'); // => '77urtle'
* replace('')('7')('7urtle'); // => 'urtle'
*
* // replace can be called both as a curried unary function or as a standard ternary function
* replace('8')('7')('7urtle') === replace('8', '7', '7urtle');
*/
export const replace = nary(replacement => substring => string => string.replace(substring, replacement));
/**
* search outputs position of input substring or regular expression withing input string or undefined if it is not found.
*
* search can be called both as a curried unary function or as a standard binary function.
*
* @HindleyMilner search :: string/regex -> string -> number
*
* @pure
* @param {string|regex} substring
* @param {string} string
* @return {number}
*
* @example
* import {search} from '@7urtle/lambda';
*
* search('7')('7urtle'); 0
* search('e')('7urtle'); // => 5
* search('rt')('7urtle'); // => 2
* search(/URT/i)('7urtle'); // => 1
* search('8')('7urtle'); => undefined
*
* // search can be called both as a curried unary function or as a standard binary function
* search('7')('7urtle') === search('7', '7urtle');
*/
export const search = nary(substring => string => minusOneToUndefined(string.search(substring)));
/**
* split outputs and array of an input string split by the input substring.
*
* split can be called both as a curried unary function or as a standard binary function.
*
* @HindleyMilner split :: string -> string -> array
*
* @pure
* @param {string} substring
* @param {string} string
* @return {array}
*
* @example
* import {split} from '@7urtle/lambda';
*
* split(' ')('7urtles are awesome'); // => ['7urtles', 'are', 'awesome']
* split('/')('7urtles are awesome'); // => ['7urtles are awesome']
*
* // split can be called both as a curried unary function or as a standard binary function
* split(' ')('7urtles are awesome') === split(' ', '7urtles are awesome');
*/
export const split = nary(substring => string => string.split(substring));
/**
* lowerCaseOf outputs the lower case version of input string.
*
* @HindleyMilner lowerCaseOf :: string -> string
*
* @pure
* @param {string} string
* @return {string}
*
* @example
* import {lowerCaseOf} from '@7urtle/lambda';
*
* lowerCaseOf('PeTrA'); // => 'petra'
* lowerCaseOf('PŘÍŠERNĚ ŽLUŤOUČKÝ KŮŇ ÚPĚL ĎÁBELSKÉ ÓDY'); // => 'příšerně žluťoučký kůň úpěl ďábelské ódy'
*/
export const lowerCaseOf = string => string.toLowerCase();
/**
* upperCaseOf outputs the upper case version of input string.
*
* @HindleyMilner upperCaseOf :: string -> string
*
* @pure
* @param {string} string
* @return {string}
*
* @example
* import {upperCaseOf} from '@7urtle/lambda';
*
* upperCaseOf('PeTrA'); // => 'PETRA'
* upperCaseOf('příšerně žluťoučký kůň úpěl ďábelské ódy'); // => 'PŘÍŠERNĚ ŽLUŤOUČKÝ KŮŇ ÚPĚL ĎÁBELSKÉ ÓDY'
*/
export const upperCaseOf = string => string.toUpperCase();