@kizmann/pico-js 1.0.13 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -7
- package/dist/pico-js.browser.js +2 -0
- package/dist/pico-js.browser.js.map +1 -0
- package/dist/pico-js.esm.js +2 -0
- package/dist/pico-js.esm.js.map +1 -0
- package/package.json +20 -7
- package/src/dom/DomAttribute.js +374 -0
- package/src/dom/DomBuilder.js +152 -0
- package/src/dom/DomEvent.js +253 -0
- package/src/dom/DomFinder.js +669 -0
- package/src/dom/DomForm.js +57 -0
- package/src/dom/DomGlobal.js +193 -0
- package/src/dom/DomInview.js +332 -0
- package/src/dom/DomMeta.js +66 -0
- package/src/dom/DomObserver.js +57 -0
- package/src/dom/DomRectangle.js +657 -0
- package/src/format/FormatFile.js +54 -0
- package/src/format/FormatOption.js +108 -0
- package/src/format/FormatParam.js +107 -0
- package/src/format/FormatParser.js +156 -0
- package/src/format/FormatUrl.js +75 -0
- package/src/index.browser.js +10 -0
- package/src/index.esm.js +138 -0
- package/src/now/NowDefault.js +533 -0
- package/src/now/NowFormat.js +196 -0
- package/src/now/NowGrid.js +251 -0
- package/src/now/NowHuman.js +118 -0
- package/src/now/NowMatch.js +175 -0
- package/src/now/NowRange.js +70 -0
- package/src/now/NowWalker.js +544 -0
- package/src/tool/scope.js +103 -0
- package/src/utils/Array.js +986 -0
- package/src/utils/Cookie.js +184 -0
- package/src/utils/Data.js +200 -0
- package/src/utils/Dom.js +208 -0
- package/src/utils/Event.js +140 -0
- package/src/utils/Format.js +62 -0
- package/src/utils/Hash.js +164 -0
- package/src/utils/Locale.js +229 -0
- package/src/utils/Mixed.js +887 -0
- package/src/utils/Now.js +234 -0
- package/src/utils/Number.js +238 -0
- package/src/utils/Object.js +655 -0
- package/src/utils/Route.js +67 -0
- package/src/utils/Runner.js +327 -0
- package/src/utils/String.js +618 -0
- package/src/{library/element.js → wip/Element.js} +90 -16
- package/src/{library/map.js → wip/Map.js} +256 -40
- package/types/dom/DomAttribute.d.ts +137 -0
- package/types/dom/DomBuilder.d.ts +67 -0
- package/types/dom/DomEvent.d.ts +103 -0
- package/types/dom/DomFinder.d.ts +321 -0
- package/types/dom/DomForm.d.ts +21 -0
- package/types/dom/DomGlobal.d.ts +79 -0
- package/types/dom/DomInview.d.ts +114 -0
- package/types/dom/DomMeta.d.ts +29 -0
- package/types/dom/DomObserver.d.ts +21 -0
- package/types/dom/DomRectangle.d.ts +270 -0
- package/types/format/FormatFile.d.ts +18 -0
- package/types/format/FormatOption.d.ts +40 -0
- package/types/format/FormatParam.d.ts +39 -0
- package/types/format/FormatParser.d.ts +46 -0
- package/types/format/FormatUrl.d.ts +17 -0
- package/types/index.browser.d.ts +1 -0
- package/types/index.esm.d.ts +52 -0
- package/types/now/NowDefault.d.ts +183 -0
- package/types/now/NowFormat.d.ts +70 -0
- package/types/now/NowGrid.d.ts +107 -0
- package/types/now/NowHuman.d.ts +37 -0
- package/types/now/NowMatch.d.ts +108 -0
- package/types/now/NowRange.d.ts +21 -0
- package/types/now/NowWalker.d.ts +301 -0
- package/types/tool/scope.d.ts +24 -0
- package/types/utils/Array.d.ts +480 -0
- package/types/utils/Cookie.d.ts +60 -0
- package/types/utils/Data.d.ts +91 -0
- package/types/utils/Dom.d.ts +138 -0
- package/types/utils/Event.d.ts +58 -0
- package/types/utils/Format.d.ts +37 -0
- package/types/utils/Hash.d.ts +81 -0
- package/types/utils/Locale.d.ts +115 -0
- package/types/utils/Mixed.d.ts +469 -0
- package/types/utils/Now.d.ts +125 -0
- package/types/utils/Number.d.ts +127 -0
- package/types/utils/Object.d.ts +255 -0
- package/types/utils/Route.d.ts +37 -0
- package/types/utils/Runner.d.ts +139 -0
- package/types/utils/String.d.ts +330 -0
- package/types/wip/Element.d.ts +119 -0
- package/types/wip/Map.d.ts +254 -0
- package/dist/.ignore.js +0 -0
- package/dist/pico-js.js +0 -2
- package/dist/pico-js.js.map +0 -1
- package/src/element/default.js +0 -46
- package/src/element/example.js +0 -58
- package/src/index.js +0 -90
- package/src/library/cookie.js +0 -123
- package/src/library/data.js +0 -111
- package/src/library/event.js +0 -91
- package/src/library/locale.js +0 -84
- package/src/library/queue.js +0 -64
- package/src/library/route.js +0 -28
- package/src/utility/any.js +0 -369
- package/src/utility/array.js +0 -410
- package/src/utility/dom.js +0 -1425
- package/src/utility/now.js +0 -544
- package/src/utility/number.js +0 -128
- package/src/utility/object.js +0 -429
- package/src/utility/string.js +0 -328
- package/types/index.d.ts +0 -77
- package/types/library/cookie.d.ts +0 -10
- package/types/library/data.d.ts +0 -15
- package/types/library/element.d.ts +0 -22
- package/types/library/event.d.ts +0 -13
- package/types/library/locale.d.ts +0 -14
- package/types/library/map.d.ts +0 -43
- package/types/library/queue.d.ts +0 -18
- package/types/library/route.d.ts +0 -11
- package/types/utility/any.d.ts +0 -35
- package/types/utility/array.d.ts +0 -46
- package/types/utility/dom.d.ts +0 -101
- package/types/utility/now.d.ts +0 -79
- package/types/utility/number.d.ts +0 -17
- package/types/utility/object.d.ts +0 -29
- package/types/utility/string.d.ts +0 -26
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import { Arr, For, Locale, Mix, Now, Str } from "#src/index.esm.js";
|
|
2
|
+
|
|
3
|
+
export class PicoString
|
|
4
|
+
{
|
|
5
|
+
/**
|
|
6
|
+
* Get character at index
|
|
7
|
+
*
|
|
8
|
+
* @example Str.get("abc", 1) // => "b"
|
|
9
|
+
*
|
|
10
|
+
* @param {any} value Input string
|
|
11
|
+
* @param {any} index Char index
|
|
12
|
+
* @returns {string} Single char
|
|
13
|
+
*/
|
|
14
|
+
static get(value, index)
|
|
15
|
+
{
|
|
16
|
+
if ( ! Mix.isStr(value) ) {
|
|
17
|
+
value = Mix.str(value);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if ( Mix.isInt(index) ) {
|
|
21
|
+
index = Mix.int(index);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return value.charAt(index);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Replace substring at index
|
|
29
|
+
*
|
|
30
|
+
* @example Str.set("abc", 1, "X") // => "aXc"
|
|
31
|
+
*
|
|
32
|
+
* @param {any} value Input string
|
|
33
|
+
* @param {any} index Start index
|
|
34
|
+
* @param {string} [replace] Replace value
|
|
35
|
+
* @returns {string} Updated string
|
|
36
|
+
*/
|
|
37
|
+
static set(value, index, replace = '')
|
|
38
|
+
{
|
|
39
|
+
if ( ! Mix.isStr(value) ) {
|
|
40
|
+
value = Mix.str(value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if ( Mix.isInt(index) ) {
|
|
44
|
+
index = Mix.int(index);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return value.substring(0, index) + replace +
|
|
48
|
+
value.substring(index + replace.length);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Replace first occurrence
|
|
53
|
+
*
|
|
54
|
+
* @example Str.replace("abc", "X", "b") // => "aXc"
|
|
55
|
+
*
|
|
56
|
+
* @param {any} value Input string
|
|
57
|
+
* @param {string} replace Replace value
|
|
58
|
+
* @param {any} [search] Search value
|
|
59
|
+
* @returns {string} Updated string
|
|
60
|
+
*/
|
|
61
|
+
static replace(value, replace, search = null)
|
|
62
|
+
{
|
|
63
|
+
if ( Mix.isNull(search) ) {
|
|
64
|
+
search = value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let index = search.indexOf(replace);
|
|
68
|
+
|
|
69
|
+
if ( index === -1 ) {
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return this.set(value, index, replace);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Extract substring by range
|
|
78
|
+
*
|
|
79
|
+
* @example Str.extract("abc", "b") // => "b"
|
|
80
|
+
*
|
|
81
|
+
* @param {any} value Input string
|
|
82
|
+
* @param {string} replace Search value
|
|
83
|
+
* @param {any} [search] Match source
|
|
84
|
+
* @returns {string|null} Extracted string
|
|
85
|
+
*/
|
|
86
|
+
static extract(value, replace, search = null)
|
|
87
|
+
{
|
|
88
|
+
if ( Mix.isNull(search) ) {
|
|
89
|
+
search = value;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let index = this.range(search, replace);
|
|
93
|
+
|
|
94
|
+
if ( index === null ) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return this.slice(value, ...index);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Match pattern in string
|
|
103
|
+
*
|
|
104
|
+
* @example Str.match("abc", /b/) // => ["b"]
|
|
105
|
+
*
|
|
106
|
+
* @param {any} value Input string
|
|
107
|
+
* @param {RegExp|string} pattern Match pattern
|
|
108
|
+
* @param {any} [cb] Callback fn
|
|
109
|
+
* @returns {any} Match result
|
|
110
|
+
*/
|
|
111
|
+
static match(value, pattern, cb = null)
|
|
112
|
+
{
|
|
113
|
+
let result = Mix.str(value).match(pattern);
|
|
114
|
+
|
|
115
|
+
if ( Mix.isEmpty(result) ) {
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return Mix.isFunc(cb) ? cb(result) : result;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Escape regex characters
|
|
124
|
+
*
|
|
125
|
+
* @example Str.regex("a.b") // => "a\\.b"
|
|
126
|
+
*
|
|
127
|
+
* @param {any} val Input string
|
|
128
|
+
* @returns {string} Escaped string
|
|
129
|
+
*/
|
|
130
|
+
static regex(val)
|
|
131
|
+
{
|
|
132
|
+
return Mix.str(val).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Convert to uppercase
|
|
137
|
+
*
|
|
138
|
+
* @example Str.uc("abc") // => "ABC"
|
|
139
|
+
*
|
|
140
|
+
* @param {any} value Input string
|
|
141
|
+
* @returns {string} Uppercase string
|
|
142
|
+
*/
|
|
143
|
+
static uc(value)
|
|
144
|
+
{
|
|
145
|
+
return Mix.str(value).toUpperCase();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @see PicoString.uc
|
|
150
|
+
*/
|
|
151
|
+
static upper = PicoString.uc;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Uppercase first character
|
|
155
|
+
*
|
|
156
|
+
* @example Str.ucfirst("abc") // => "Abc"
|
|
157
|
+
*
|
|
158
|
+
* @param {string} value Input string
|
|
159
|
+
* @returns {string} Updated string
|
|
160
|
+
*/
|
|
161
|
+
static ucfirst(value)
|
|
162
|
+
{
|
|
163
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Convert to lowercase
|
|
168
|
+
*
|
|
169
|
+
* @example Str.lc("ABC") // => "abc"
|
|
170
|
+
*
|
|
171
|
+
* @param {any} value Input string
|
|
172
|
+
* @returns {string} Lowercase string
|
|
173
|
+
*/
|
|
174
|
+
static lc(value)
|
|
175
|
+
{
|
|
176
|
+
return Mix.str(value).toLowerCase();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* @see PicoString.lc
|
|
181
|
+
*/
|
|
182
|
+
static lower = PicoString.lc;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Lowercase first character
|
|
186
|
+
*
|
|
187
|
+
* @example Str.lcfirst("ABC") // => "aBC"
|
|
188
|
+
*
|
|
189
|
+
* @param {string} value Input string
|
|
190
|
+
* @returns {string} Updated string
|
|
191
|
+
*/
|
|
192
|
+
static lcfirst(value)
|
|
193
|
+
{
|
|
194
|
+
return value.charAt(0).toLowerCase() + value.slice(1);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Convert to camelCase
|
|
199
|
+
*
|
|
200
|
+
* @example Str.cc("hello-world") // => "helloWorld"
|
|
201
|
+
*
|
|
202
|
+
* @param {string} value Input string
|
|
203
|
+
* @returns {string} camelCase string
|
|
204
|
+
*/
|
|
205
|
+
static cc(value)
|
|
206
|
+
{
|
|
207
|
+
let splits = Arr.each(For.slugify(value).split('-'), (v, i) => {
|
|
208
|
+
return i ? this.ucfirst(v) : v;
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
return splits.join('');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @see PicoString.cc
|
|
216
|
+
*/
|
|
217
|
+
static camelcase = PicoString.cc;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Convert to kebab-case
|
|
221
|
+
*
|
|
222
|
+
* @example Str.kc("helloWorld") // => "hello-world"
|
|
223
|
+
*
|
|
224
|
+
* @param {string} value Input string
|
|
225
|
+
* @returns {string} kebab-case string
|
|
226
|
+
*/
|
|
227
|
+
static kc(value)
|
|
228
|
+
{
|
|
229
|
+
return For.slugify(value);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @see PicoString.kc
|
|
234
|
+
*/
|
|
235
|
+
static kebabcase = PicoString.kc;
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Convert to snake_case
|
|
239
|
+
*
|
|
240
|
+
* @example Str.sc("helloWorld") // => "hello_world"
|
|
241
|
+
*
|
|
242
|
+
* @param {string} value Input string
|
|
243
|
+
* @returns {string} snake_case string
|
|
244
|
+
*/
|
|
245
|
+
static sc(value)
|
|
246
|
+
{
|
|
247
|
+
return For.slugify(value).replace('-', '_');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* @see PicoString.sc
|
|
252
|
+
*/
|
|
253
|
+
static snakecase = PicoString.sc;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Convert to PascalCase
|
|
257
|
+
*
|
|
258
|
+
* @example Str.pc("hello-world") // => "HelloWorld"
|
|
259
|
+
*
|
|
260
|
+
* @param {string} value Input string
|
|
261
|
+
* @returns {string} PascalCase string
|
|
262
|
+
*/
|
|
263
|
+
static pc(value)
|
|
264
|
+
{
|
|
265
|
+
let splits = Arr.each(For.slugify(value).split('-'), (v) => {
|
|
266
|
+
return this.ucfirst(v);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
return splits.join('');
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* @see PicoString.pc
|
|
274
|
+
*/
|
|
275
|
+
static pascalcase = PicoString.pc;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Check if string contains value
|
|
279
|
+
*
|
|
280
|
+
* @example Str.has("abc", "b") // => true
|
|
281
|
+
*
|
|
282
|
+
* @param {string} value Input string
|
|
283
|
+
* @param {string} search Search string
|
|
284
|
+
* @returns {boolean} True if found
|
|
285
|
+
*/
|
|
286
|
+
static has(value, search)
|
|
287
|
+
{
|
|
288
|
+
return this.lc(value).indexOf(this.lc(search)) !== -1;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Get index range of search
|
|
293
|
+
*
|
|
294
|
+
* @example Str.range("abc", "b") // => [1, 2]
|
|
295
|
+
*
|
|
296
|
+
* @param {any} value Input string
|
|
297
|
+
* @param {string} search Search string
|
|
298
|
+
* @param {any} [fallback] Fallback value
|
|
299
|
+
* @returns {Array<number>|any} Index range
|
|
300
|
+
*/
|
|
301
|
+
static range(value, search, fallback = null)
|
|
302
|
+
{
|
|
303
|
+
let index = Mix.str(value).indexOf(search);
|
|
304
|
+
|
|
305
|
+
if ( index === -1 ) {
|
|
306
|
+
return fallback;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return [index, index + search.length];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Extract substring by range
|
|
314
|
+
*
|
|
315
|
+
* @example Str.slice("abc", 1, 2) // => "b"
|
|
316
|
+
*
|
|
317
|
+
* @param {any} value Input string
|
|
318
|
+
* @param {number} start Start index
|
|
319
|
+
* @param {number} limit End index
|
|
320
|
+
* @returns {string} Sliced string
|
|
321
|
+
*/
|
|
322
|
+
static slice(value, start, limit)
|
|
323
|
+
{
|
|
324
|
+
return Mix.str(value).slice(start, limit);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Get string representation
|
|
329
|
+
*
|
|
330
|
+
* @example Str.string("<b>a</b>") // => "a"
|
|
331
|
+
*
|
|
332
|
+
* @param {any} value Input value
|
|
333
|
+
* @param {any} [empty] Empty fallback
|
|
334
|
+
* @param {boolean} [html] Keep HTML
|
|
335
|
+
* @returns {string} String value
|
|
336
|
+
*/
|
|
337
|
+
static string(value, empty = '-', html = false)
|
|
338
|
+
{
|
|
339
|
+
if ( Mix.isEmpty(value) ) {
|
|
340
|
+
return empty;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
value = Mix.str(value);
|
|
344
|
+
|
|
345
|
+
if ( html ) {
|
|
346
|
+
return value;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return value.replace(/<[^>]*>?/gm, '');
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Get boolean representation
|
|
354
|
+
*
|
|
355
|
+
* @example Str.boolean(true) // => "Yes"
|
|
356
|
+
*
|
|
357
|
+
* @param {any} value Input value
|
|
358
|
+
* @param {string} [yes] True string
|
|
359
|
+
* @param {string} [no] False string
|
|
360
|
+
* @param {any} [empty] Empty fallback
|
|
361
|
+
* @returns {string|any} Boolean string
|
|
362
|
+
*/
|
|
363
|
+
static boolean(value, yes = 'Yes', no = 'No', empty = '-')
|
|
364
|
+
{
|
|
365
|
+
if ( Mix.isEmpty(value) ) {
|
|
366
|
+
return empty;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return Mix.bool(value) ? yes : no;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Get formatted number
|
|
374
|
+
*
|
|
375
|
+
* @example Str.number(1234.56, 1) // => "1,234.6"
|
|
376
|
+
*
|
|
377
|
+
* @param {any} value Input value
|
|
378
|
+
* @param {number} [fixed] Decimal points
|
|
379
|
+
* @param {string} [locale] Locale code
|
|
380
|
+
* @param {any} [config] Format config
|
|
381
|
+
* @returns {string|any} Formatted string
|
|
382
|
+
*/
|
|
383
|
+
static number(value, fixed = null, locale = null, config = {})
|
|
384
|
+
{
|
|
385
|
+
if ( ! Mix.isNum(value) ) {
|
|
386
|
+
return value;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if ( locale == null ) {
|
|
390
|
+
locale = Locale.code();
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
config = {
|
|
394
|
+
maximumFractionDigits: 12, ...config
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
if ( fixed != null ) {
|
|
398
|
+
config.minimumFractionDigits = fixed;
|
|
399
|
+
config.maximumFractionDigits = fixed;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return Mix.num(value).toLocaleString(locale, config);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Get formatted integer
|
|
407
|
+
*
|
|
408
|
+
* @example Str.integer(1, 2) // => "01"
|
|
409
|
+
*
|
|
410
|
+
* @param {any} value Input value
|
|
411
|
+
* @param {number} [leading] Leading zeros
|
|
412
|
+
* @param {string} [padding] Padding char
|
|
413
|
+
* @returns {string|any} Formatted string
|
|
414
|
+
*/
|
|
415
|
+
static integer(value, leading = 0, padding = '0')
|
|
416
|
+
{
|
|
417
|
+
if ( ! Mix.isNum(value) ) {
|
|
418
|
+
return value;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if ( ! Mix.isInt(value) ) {
|
|
422
|
+
value = Mix.int(value);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
value = Mix.int(value);
|
|
426
|
+
|
|
427
|
+
if ( ! leading ) {
|
|
428
|
+
return Mix.str(value);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
return Mix.str(value).padStart(leading, padding);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Get formatted datetime
|
|
436
|
+
*
|
|
437
|
+
* @example Str.datetime("2026-01-01", "DD.MM.YYYY")
|
|
438
|
+
*
|
|
439
|
+
* @param {any} value Input value
|
|
440
|
+
* @param {string} [format] Output format
|
|
441
|
+
* @param {any} [empty] Empty fallback
|
|
442
|
+
* @returns {string} Formatted string
|
|
443
|
+
*/
|
|
444
|
+
static datetime(value, format = 'YYYY-MM-DD HH:mm:ss', empty = '-')
|
|
445
|
+
{
|
|
446
|
+
if ( ! Mix.isDate(value) ) {
|
|
447
|
+
value = Now.make(value);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if ( ! value.valid() ) {
|
|
451
|
+
return empty;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return value.format(format);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Get formatted date
|
|
459
|
+
*
|
|
460
|
+
* @example Str.date("2026-01-01", "DD.MM.YYYY")
|
|
461
|
+
*
|
|
462
|
+
* @param {any} value Input value
|
|
463
|
+
* @param {string} [format] Output format
|
|
464
|
+
* @param {any} [empty] Empty fallback
|
|
465
|
+
* @returns {string} Formatted string
|
|
466
|
+
*/
|
|
467
|
+
static date(value, format = 'YYYY-MM-DD', empty = '-')
|
|
468
|
+
{
|
|
469
|
+
return this.datetime(value, format, empty);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Get formatted time
|
|
474
|
+
*
|
|
475
|
+
* @example Str.time("12:00:00", "HH:mm")
|
|
476
|
+
*
|
|
477
|
+
* @param {any} value Input value
|
|
478
|
+
* @param {string} [format] Output format
|
|
479
|
+
* @param {any} [empty] Empty fallback
|
|
480
|
+
* @returns {string} Formatted string
|
|
481
|
+
*/
|
|
482
|
+
static time(value, format = 'HH:mm:ss', empty = '-')
|
|
483
|
+
{
|
|
484
|
+
return this.datetime(value, format, empty);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Parse string to object
|
|
489
|
+
*
|
|
490
|
+
* @example Str.objectify("a: 1;") // => { a: 1 }
|
|
491
|
+
*
|
|
492
|
+
* @param {any} value Input string
|
|
493
|
+
* @param {string} [mode] Parse mode
|
|
494
|
+
* @param {boolean} [toarray] Return array
|
|
495
|
+
* @returns {any} Parsed value
|
|
496
|
+
*/
|
|
497
|
+
static objectify(value, mode = 'options', toarray = false)
|
|
498
|
+
{
|
|
499
|
+
if ( Mix.isRef(value) ) {
|
|
500
|
+
return value;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if ( mode === 'params' ) {
|
|
504
|
+
value = For.parseParams(value);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if ( mode === 'options' ) {
|
|
508
|
+
value = For.parseOptions(value);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
if ( ! Mix.isRef(value) ) {
|
|
512
|
+
value = JSON.parse(value)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
return toarray ? Mix.vals(value) : value;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Cast object to string
|
|
520
|
+
*
|
|
521
|
+
* @example Str.stringify({ a: 1 }) // => "a: 1;"
|
|
522
|
+
*
|
|
523
|
+
* @param {any} value Input value
|
|
524
|
+
* @param {string} [mode] Cast mode
|
|
525
|
+
* @returns {string} Casted string
|
|
526
|
+
*/
|
|
527
|
+
static stringify(value, mode = 'options')
|
|
528
|
+
{
|
|
529
|
+
if ( Mix.isStr(value) ) {
|
|
530
|
+
return value;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if ( mode === 'params' ) {
|
|
534
|
+
return For.castParams(value);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if ( mode === 'options' ) {
|
|
538
|
+
return For.castOptions(value);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
return JSON.stringify(value);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* @see PicoStr.regex
|
|
548
|
+
*/
|
|
549
|
+
PicoString.regexEscape = (...args) => {
|
|
550
|
+
console.warn('Str.regexEscape() is deprecated, use Str.regex() instead.');
|
|
551
|
+
return Str.regex(...args);
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* @see PicoStr.pascalcase
|
|
556
|
+
*/
|
|
557
|
+
PicoString.humancase = (...args) => {
|
|
558
|
+
console.warn('Str.humancase() is deprecated, use Str.pascalcase() instead.');
|
|
559
|
+
return Str.pascalcase(...args);
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* @see PicoFormat.slugify
|
|
564
|
+
*/
|
|
565
|
+
PicoString.slugify = (...args) => {
|
|
566
|
+
console.warn('Str.slugify() is deprecated, use For.slugify() instead.');
|
|
567
|
+
return For.slugify(...args);
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* @see PicoFormat.castOptions
|
|
572
|
+
*/
|
|
573
|
+
PicoString.options = (...args) => {
|
|
574
|
+
console.warn('Str.options() is deprecated, use For.castOptions() instead.');
|
|
575
|
+
return For.castOptions(...args);
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* @see PicoFormat.parseOptions
|
|
580
|
+
*/
|
|
581
|
+
PicoString.fromOptions = (...args) => {
|
|
582
|
+
console.warn('Str.fromOptions() is deprecated, use For.parseOptions() instead.');
|
|
583
|
+
return For.parseOptions(...args);
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* @see PicoFormat.castParams
|
|
588
|
+
*/
|
|
589
|
+
PicoString.params = (...args) => {
|
|
590
|
+
console.warn('Str.params() is deprecated, use For.castParams() instead.');
|
|
591
|
+
return For.castParams(...args);
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* @see PicoFormat.parseParams
|
|
596
|
+
*/
|
|
597
|
+
PicoString.fromParams = (...args) => {
|
|
598
|
+
console.warn('Str.fromParams() is deprecated, use For.parseParams() instead.');
|
|
599
|
+
return For.parseParams(...args);
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* @see PicoFormat.filesize
|
|
604
|
+
*/
|
|
605
|
+
PicoString.filesize = (...args) => {
|
|
606
|
+
console.warn('Str.filesize() is deprecated, use For.filesize() instead.');
|
|
607
|
+
return For.filesize(...args);
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
PicoString.real = () => {
|
|
611
|
+
console.error('Str.real() is not implemented anymore.');
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
PicoString.array = () => {
|
|
615
|
+
console.error('Str.array() is not implemented anymore.');
|
|
616
|
+
};
|
|
617
|
+
|
|
618
|
+
export default PicoString;
|