-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.js
More file actions
305 lines (232 loc) · 7.09 KB
/
Copy pathstr.js
File metadata and controls
305 lines (232 loc) · 7.09 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/javascript/blob/master/LICENSE
*/
// str
// script with a set of helper functions related to strings
// import
import { Arr, Html, Integer, Json, Num, Obj, Pojo } from '../index.js';
// export
export default {
// is
// retourne vrai si la valeur est une string
is: function(value)
{
return typeof(value) === 'string';
},
// isStart
// retourne vrai si la string commence par needle
isStart: function(needle,value)
{
return (this.is(needle) && this.is(value))? value.startsWith(needle):null;
},
// isEnd
// retourne vrai si la string finit par needle
isEnd: function(needle,value)
{
return (this.is(needle) && this.is(value))? value.endsWith(needle):null;
},
// isEqual
// retourne vrai si les deux valeurs sont égales si comparés comme string
isEqual: function(value,value2)
{
return this.cast(value) === this.cast(value2);
},
// in
// retourne vrai si la valeur est dans la string
// retourne un boolean
in: function(value,string)
{
return (this.is(string) && this.is(value))? string.includes(value):null;
},
// icompare
// compare deux string de façon insensible à la case
icompare: function(value,value2)
{
return (this.is(value) && this.is(value2))? (value.toUpperCase() === value2.toUpperCase()):false
},
// cast
// retourne une valeur string
// si la valeur est null retourne ''
// si la valeur est objet, et que json est true -> envoie à json encode
cast: function(value,json)
{
let r = '';
if(value != null)
{
if(Obj.is(value) && json === true)
r = Json.encode(value);
else
r = String(value)
}
return r;
},
// toNum
// retourne la string sous forme de nombre
toNum: function(value)
{
this.typecheck(value);
return Num.cast(value);
},
// toInt
// retourne la string sous forme de integer
toInt: function(value)
{
this.typecheck(value);
return Integer.cast(value);
},
// pos
// retourne l'index de la valeur dans la string
pos: function(value,string)
{
this.typecheck(string);
let r = string.indexOf(value);
r = (r === -1)? null:r;
return r;
},
// lower
// retourne la chaîne en lower case
lower: function(value)
{
this.typecheck(value);
return value.toLowerCase();
},
// lowerFirst
// met la première lettre de la string lowercase
lowerFirst: function(value)
{
this.typecheck(value);
return (this.isNotEmpty(value))? value.charAt(0).toLowerCase() + value.slice(1):null;
},
// upper
// retourne la chaîne en uppercase
upper: function(value)
{
this.typecheck(value);
return value.toUpperCase();
},
// upperFirst
// met la première lettre de la string uppercase
upperFirst: function(value)
{
this.typecheck(value);
return (this.isNotEmpty(value))? value.charAt(0).toUpperCase() + value.slice(1):null;
},
// trim
// trim une string
trim: function(value)
{
this.typecheck(value);
return value.trim();
},
// quote
// permet d'enrobber une string dans des quotes
// possible de spécifier double ou non
// possible de faire un escape html
quote: function(value,double,escape)
{
let r = null;
this.typecheck(value);
const quote = (double === true)? '"':"'";
if(escape === true)
value = Html.escape(value);
r = quote+value+quote;
return r;
},
// sub
// retourne une nouvelle sous chaîne
sub: function(start,end,string)
{
this.typecheck(string);
Integer.typecheck(start);
return string.substring(start,(end === true)? undefined:end);
},
// excerpt
// retourne une nouvelle sous chaîne
// peut ajouter un suffix si la chaîne a été coupé
excerpt: function(limit,string,suffix)
{
let r = this.sub(0,limit,string);
if(r !== string && this.isNotEmpty(suffix))
r += suffix;
return r;
},
// explode
// explode une chaîne
// retourne un tableau dans tous les cas
explode: function(delimiter,value,clean)
{
this.typechecks([delimiter,value]);
let r = value.split(delimiter);
if(clean === true)
r = Arr.clean(r);
return r;
},
// explodeIndex
// split une string et retourne l'index demandé en premier argument
explodeIndex: function(index,delimiter,value)
{
let r = undefined;
const x = this.explode(delimiter,value);
if(Integer.is(index) && this.isNotEmpty(x[index]))
r = x[index];
return r;
},
// removeAllWhitespace
// enlève tous les espaces blancs d'une string
removeAllWhitespace: function(string)
{
this.typecheck(string);
return string.replace(/\s/g, "");
},
// fromCamelCase
// transforme une string camelcase vers une string avec séparateur
fromCamelCase: function(delimiter,string)
{
this.typecheck(delimiter);
string = this.trim(string);
return string.replace(/[\w]([A-Z])/g, function(value) {
return value[0] + delimiter + value[1];
}).toLowerCase();
},
// toCamelCase
// transforme une string avec séparateur en camelCase
toCamelCase: function(delimiter,string)
{
let r = null;
const $inst = this;
string = this.trim(string);
let array = this.explode(delimiter,string,true);
array = Arr.map(array,function(word,index) {
return (index == 0)? $inst.lower(word):$inst.upperFirst(word);
});
r = array.join('');
r = this.removeAllWhitespace(r);
return r;
},
// slug
// transforme une string en slug
slug: function(string)
{
string = this.lower(string);
return string.replace(/ /g,'-').replace(/[^\w-]+/g,'').replace(/--/g,'-');
},
// keepNumber
// enleve tous les caractères non numérique
keepNumber: function(string)
{
this.typecheck(string);
return string.replace(/[^0-9]/g,'');
},
// replace
// permet de remplacer le contenu d'une string via un pojo
replace: function(pojo,string)
{
this.typecheck(string);
Pojo.each(pojo,function(value,key) {
string = string.replace(key,value);
});
return string;
}
}