1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.stringPascalCase = exports.stringCamelCase = exports.CC_TO_UP = exports.CC_TO_LO = void 0;
|
7 | // Copyright 2017-2022 @polkadot/util authors & contributors
|
8 | // SPDX-License-Identifier: Apache-2.0
|
9 | const CC_TO_UP = new Array(256);
|
10 | exports.CC_TO_UP = CC_TO_UP;
|
11 | const CC_TO_LO = new Array(256);
|
12 | exports.CC_TO_LO = CC_TO_LO;
|
13 |
|
14 | for (let i = 0; i < CC_TO_UP.length; i++) {
|
15 | CC_TO_LO[i] = String.fromCharCode(i).toLowerCase();
|
16 | CC_TO_UP[i] = String.fromCharCode(i).toUpperCase();
|
17 | }
|
18 | /** @internal */
|
19 |
|
20 |
|
21 | function formatAllCaps(w) {
|
22 | return w.slice(0, w.length - 1).toLowerCase() + CC_TO_UP[w.charCodeAt(w.length - 1)];
|
23 | }
|
24 | /**
|
25 | * @internal
|
26 | *
|
27 | * Inspired by https://stackoverflow.com/a/2970667
|
28 | *
|
29 | * This is not as optimal as the original SO answer (we split into per-word),
|
30 | * however it does pass the tests (which the SO version doesn't) and is still
|
31 | * a major improvement over the original camelcase npm package -
|
32 | *
|
33 | * camelcase: 20.88 μs/op
|
34 | * this: 1.00 μs/op
|
35 | *
|
36 | * Caveat of this: only Ascii, but acceptable for the intended usecase
|
37 | */
|
38 |
|
39 |
|
40 | function converter(format) {
|
41 | return value => {
|
42 | const parts = value // replace all seperators (including consequtive) with spaces
|
43 | .replace(/[-_., ]+/g, ' ') // we don't want leading or trailing spaces
|
44 | .trim() // split into words
|
45 | .split(' ');
|
46 | const count = parts.length;
|
47 | let result = '';
|
48 |
|
49 | for (let i = 0; i < count; i++) {
|
50 | const w = parts[i]; // apply the formatting
|
51 |
|
52 | result += format(/^[\dA-Z]+$/.test(w) // all full uppercase + letters are changed to lowercase
|
53 | ? w.toLowerCase() // all consecutive capitals + letters are changed to lowercase
|
54 | // e.g. UUID64 -> uuid64, while preserving splits, eg. NFTOrder -> nftOrder
|
55 | : w.replace(/^[\dA-Z]{2,}[^a-z]/, formatAllCaps), i);
|
56 | }
|
57 |
|
58 | return result;
|
59 | };
|
60 | }
|
61 | /**
|
62 | * @name stringCamelCase
|
63 | * @summary Convert a dash/dot/underscore/space separated Ascii string/String to camelCase
|
64 | */
|
65 |
|
66 |
|
67 | const stringCamelCase = converter((w, i) => // lowercase for first letter/first word, else uppercase first, rest unchanged
|
68 | (i ? CC_TO_UP[w.charCodeAt(0)] : CC_TO_LO[w.charCodeAt(0)]) + w.slice(1));
|
69 | /**
|
70 | * @name stringPascalCase
|
71 | * @summary Convert a dash/dot/underscore/space separated Ascii string/String to PascalCase
|
72 | */
|
73 |
|
74 | exports.stringCamelCase = stringCamelCase;
|
75 | const stringPascalCase = converter(w => // uppercase the first character, leave the rest unchanged
|
76 | CC_TO_UP[w.charCodeAt(0)] + w.slice(1));
|
77 | exports.stringPascalCase = stringPascalCase; |
\ | No newline at end of file |