stringweaver 1.4.0 → 1.4.3
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/Bundle/index.min.js +2 -2
- package/Bundle/index.min.js.map +4 -4
- package/index.js +2 -3
- package/package.json +1 -1
- package/src/CTORCreator.js +21 -0
- package/src/Factories/regExpFromMultilineStringFactory.js +1 -2
- package/src/Factories/{interpolateFactory.js → splatESBundle.js} +113 -41
- package/src/{genericMethods.js → helpers.js} +153 -147
- package/src/{extensions.js → instanceCreator.js} +2 -3
- package/src/instanceMethods.js +5 -39
- package/tests/UnitTests.js +32 -27
|
@@ -1,14 +1,28 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
1
|
+
const IS = typeCheckFactory();
|
|
2
|
+
const interpolateDefault = interpolateFactory();
|
|
3
|
+
const interpolateClear = interpolateFactory("");
|
|
4
|
+
|
|
5
|
+
export {
|
|
6
|
+
interpolateDefault as default,
|
|
7
|
+
interpolateClear,
|
|
8
|
+
addSymbolicStringExtensions,
|
|
9
|
+
interpolateFactory,
|
|
10
|
+
};
|
|
3
11
|
|
|
4
12
|
/**
|
|
5
13
|
* Factory function to create an interpolate function with a default replacer.
|
|
6
|
-
* @param {string} defaultReplacer - Default value to use for missing tokens.
|
|
14
|
+
* @param {string|number} defaultReplacer - Default value to use for missing tokens.
|
|
7
15
|
* @returns {Function} - The interpolation function.
|
|
8
16
|
*/
|
|
9
|
-
function interpolateFactory(defaultReplacer =
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
function interpolateFactory(defaultReplacer, specs = {}) {
|
|
18
|
+
const {useSymbolicExtensions} = specs;
|
|
19
|
+
defaultReplacer = IS(defaultReplacer, String, Number) ?
|
|
20
|
+
String(defaultReplacer) : undefined;
|
|
21
|
+
|
|
22
|
+
if (!!useSymbolicExtensions) {
|
|
23
|
+
addSymbolicStringExtensions();
|
|
24
|
+
}
|
|
25
|
+
|
|
12
26
|
/**
|
|
13
27
|
* Main interpolation function.
|
|
14
28
|
* @param {string} str - The string with placeholders.
|
|
@@ -18,7 +32,7 @@ function interpolateFactory(defaultReplacer = "") {
|
|
|
18
32
|
return function(str, ...tokens) {
|
|
19
33
|
return interpolate(str, processTokens(tokens));
|
|
20
34
|
}
|
|
21
|
-
|
|
35
|
+
|
|
22
36
|
/**
|
|
23
37
|
* Handle invalid keys by returning the default replacer or the key in braces.
|
|
24
38
|
* @param {string} key - The placeholder key.
|
|
@@ -26,22 +40,13 @@ function interpolateFactory(defaultReplacer = "") {
|
|
|
26
40
|
* @returns {string} - The replacement value.
|
|
27
41
|
*/
|
|
28
42
|
function invalidate(key, keyExists) {
|
|
29
|
-
if (keyExists && defaultReplacer
|
|
43
|
+
if (keyExists && IS(defaultReplacer, String, Number)) {
|
|
30
44
|
return String(defaultReplacer);
|
|
31
45
|
}
|
|
32
|
-
|
|
46
|
+
|
|
33
47
|
return `{${key}}`;
|
|
34
48
|
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* determine if [value] is a String or Number
|
|
38
|
-
* @param {any} value
|
|
39
|
-
* @returns {boolean}
|
|
40
|
-
*/
|
|
41
|
-
function isStringOrNumber(value ) {
|
|
42
|
-
return value?.constructor === String || value?.constructor === Number && !Number.isNaN(value);
|
|
43
|
-
}
|
|
44
|
-
|
|
49
|
+
|
|
45
50
|
/**
|
|
46
51
|
* Get the replacement value for a key from the token.
|
|
47
52
|
* @param {string} key - The placeholder key.
|
|
@@ -50,9 +55,9 @@ function interpolateFactory(defaultReplacer = "") {
|
|
|
50
55
|
*/
|
|
51
56
|
function replacement(key, token) {
|
|
52
57
|
const isValid = key in token;
|
|
53
|
-
return isValid &&
|
|
58
|
+
return isValid && IS(token[key], String, Number) ? String(token[key]) : invalidate(key, isValid);
|
|
54
59
|
}
|
|
55
|
-
|
|
60
|
+
|
|
56
61
|
/**
|
|
57
62
|
* Create a lambda function for replacing placeholders in the string.
|
|
58
63
|
* @param {object} token - The token object containing replacement values.
|
|
@@ -64,7 +69,7 @@ function interpolateFactory(defaultReplacer = "") {
|
|
|
64
69
|
return replacement((replacementObject ? replacementObject.key : `_`), token);
|
|
65
70
|
};
|
|
66
71
|
}
|
|
67
|
-
|
|
72
|
+
|
|
68
73
|
/**
|
|
69
74
|
* Replace placeholders in the string with values from the token.
|
|
70
75
|
* @param {string} str - The string with placeholders.
|
|
@@ -74,24 +79,23 @@ function interpolateFactory(defaultReplacer = "") {
|
|
|
74
79
|
function replace(str, token) {
|
|
75
80
|
return str.replace(/\{(?<key>[a-z_\d]+)}/gim, getReplacerLambda(token));
|
|
76
81
|
}
|
|
77
|
-
|
|
82
|
+
|
|
78
83
|
/**
|
|
79
84
|
* Convert token object to array of token Objects
|
|
80
85
|
* when it's values are arrays of values.
|
|
81
86
|
* @param {object} tokenObject - The token object containing arrays of values.
|
|
82
87
|
* @returns {object[]} - Array of token objects.
|
|
83
88
|
*/
|
|
84
|
-
/* node:coverage disable (internal method, not covered by tests)*/
|
|
85
89
|
function convertTokensFromArrayValues(tokenObject) {
|
|
86
90
|
const converted = [];
|
|
87
|
-
|
|
91
|
+
|
|
88
92
|
Object.entries(tokenObject).forEach(([key, value]) => {
|
|
89
93
|
value.forEach((v, i) => (converted[i] ??= {}, converted[i][key] = v));
|
|
90
94
|
});
|
|
91
|
-
|
|
95
|
+
|
|
92
96
|
return converted;
|
|
93
97
|
}
|
|
94
|
-
|
|
98
|
+
|
|
95
99
|
/**
|
|
96
100
|
* Check if single token and its values are arrays.
|
|
97
101
|
* @param {object[]} tokens - The tokens to check.
|
|
@@ -101,7 +105,7 @@ function interpolateFactory(defaultReplacer = "") {
|
|
|
101
105
|
function isMultiLineWithArrays(tokens) {
|
|
102
106
|
return tokens.length === 1 && Object.values(tokens[0]).every(Array.isArray);
|
|
103
107
|
}
|
|
104
|
-
|
|
108
|
+
|
|
105
109
|
/**
|
|
106
110
|
* Process tokens to handle multi-line formats.
|
|
107
111
|
* @param {object[]} tokens - The tokens to process.
|
|
@@ -110,16 +114,7 @@ function interpolateFactory(defaultReplacer = "") {
|
|
|
110
114
|
function processTokens(tokens) {
|
|
111
115
|
return isMultiLineWithArrays(tokens) ? convertTokensFromArrayValues(tokens[0]) : tokens;
|
|
112
116
|
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Determine [value] is a real Object and contains keys and values
|
|
116
|
-
* @param {any} value
|
|
117
|
-
* @returns {boolean}
|
|
118
|
-
*/
|
|
119
|
-
function isKeyValueObject(value) {
|
|
120
|
-
return !Array.isArray(value) && value?.constructor === Object && Object.entries(value)?.length > 0;
|
|
121
|
-
}
|
|
122
|
-
|
|
117
|
+
|
|
123
118
|
/**
|
|
124
119
|
* Interpolate the string with the given tokens.
|
|
125
120
|
* @param {string} str - The string with placeholders.
|
|
@@ -127,9 +122,86 @@ function interpolateFactory(defaultReplacer = "") {
|
|
|
127
122
|
* @returns {string} - The interpolated string.
|
|
128
123
|
*/
|
|
129
124
|
function interpolate(str, tokens) {
|
|
130
|
-
|
|
131
|
-
.filter(token => token)
|
|
132
|
-
.map((token, i) =>
|
|
125
|
+
const injected = !tokens?.length ? str : tokens
|
|
126
|
+
.filter(token => IS(token, Object))
|
|
127
|
+
.map((token, i) => replace(str, {...token, index: i + 1}))
|
|
133
128
|
.join(``);
|
|
129
|
+
|
|
130
|
+
return IS(defaultReplacer, undefined)
|
|
131
|
+
? injected : injected.replace(/\{[a-z_\d].+\}/gim, String(defaultReplacer));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Extend String.prototype using the above two
|
|
137
|
+
* interpolate methods.
|
|
138
|
+
* Note: Symbols are unique, so there is no risk the
|
|
139
|
+
* methods will conflict with native String methods or
|
|
140
|
+
* methods in other ES libraries.
|
|
141
|
+
* @returns {symbol[]}
|
|
142
|
+
*/
|
|
143
|
+
function addSymbolicStringExtensions() {
|
|
144
|
+
if (!String.prototype[Symbol.for(`interpolate`)]) {
|
|
145
|
+
Object.defineProperties(String.prototype, {
|
|
146
|
+
[Symbol.for(`interpolate`)]: {
|
|
147
|
+
value(...args) {
|
|
148
|
+
return interpolateDefault(this, ...args);
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
[Symbol.for(`interpolate$`)]: {
|
|
152
|
+
value(...args) {
|
|
153
|
+
return interpolateClear(this, ...args);
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return [Symbol.for("interpolate"), Symbol.for(`interpolate$`)];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Simple 'type' checking factory
|
|
164
|
+
* @returns {function(*, ...[*]): (boolean|*)}
|
|
165
|
+
*/
|
|
166
|
+
function typeCheckFactory() {
|
|
167
|
+
const collate = new Intl.Collator(`en`, {sensitivity: 'base'});
|
|
168
|
+
const nameOf = type2Check => typeof type2Check === `function`
|
|
169
|
+
? type2Check?.name || type2Check?.constructor?.name : `noCTOR`;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* check obj to be of [type2Check]
|
|
173
|
+
* @param {*} obj
|
|
174
|
+
* @param {*} type2Check
|
|
175
|
+
* @returns {boolean}
|
|
176
|
+
*/
|
|
177
|
+
function checkSingleType(obj, type2Check) {
|
|
178
|
+
if (type2Check === Number && (Number.isNaN(obj) || !Number.isFinite(obj))) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return 0 === collate.compare(
|
|
183
|
+
Object.prototype.toString.call(obj),
|
|
184
|
+
`[object ${nameOf(type2Check)}]`
|
|
185
|
+
) || obj?.name === type2Check?.name;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
*
|
|
190
|
+
* @param {*} obj
|
|
191
|
+
* @param {*[]} type2Check
|
|
192
|
+
* @returns {boolean}
|
|
193
|
+
*/
|
|
194
|
+
function checkType(obj, ...type2Check) {
|
|
195
|
+
if (type2Check.length > 1) {
|
|
196
|
+
for (const chkType of type2Check) {
|
|
197
|
+
if (checkSingleType(obj, chkType)) { return true; }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return checkSingleType(obj, type2Check?.shift());
|
|
134
204
|
}
|
|
205
|
+
|
|
206
|
+
return checkType;
|
|
135
207
|
}
|
|
@@ -1,110 +1,92 @@
|
|
|
1
|
-
import
|
|
2
|
-
import interpolate from "./Factories/interpolateFactory.js";
|
|
1
|
+
import interpolate from "./factories/splatESBundle.js";
|
|
3
2
|
import createRegExp from "./Factories/regExpFromMultilineStringFactory.js";
|
|
4
|
-
import
|
|
5
|
-
|
|
3
|
+
import {default as randomString, uuid4} from "./Factories/randomStringFactory.js";
|
|
4
|
+
import {CustomStringConstructor} from "./CTORCreator.js";
|
|
5
|
+
import {parseCamelcase, parseKebabCase, parseSnakeCase, ucFirst, wordsFirstUp} from "./instanceMethods.js";
|
|
6
6
|
const quotingStyles = defineQuotingStyles();
|
|
7
|
+
const deprecatedRE = /symbol|anchor|big|blink|bold|fixed|fontsize|fontcolor|italics|link|small|strike|sup|sub/i
|
|
7
8
|
|
|
8
9
|
export {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
isArrayOf,
|
|
13
|
-
quotGetters4Instance,
|
|
14
|
-
getStringValue,
|
|
15
|
-
escapeRE,
|
|
16
|
-
resolveTemplateString,
|
|
17
|
-
clone,
|
|
18
|
-
interpolate,
|
|
19
|
-
quotingStyles,
|
|
20
|
-
createRegExp,
|
|
10
|
+
capitalizerFactory, createExtendedCTOR, createRegExp, defineQuotingStyles,
|
|
11
|
+
deprecatedRE, getStringValue, escapeRE, infoValue, interpolate, isArrayOf, isNumber,
|
|
12
|
+
randomString, resolveTemplateString, retrieveQuotInfo, quotGetters4Instance, uuid4,
|
|
21
13
|
};
|
|
22
14
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
`
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
:
|
|
15
|
+
function defineQuotingStyles() {
|
|
16
|
+
// see https://en.wikipedia.org/wiki/Quotation_mark
|
|
17
|
+
const quots = {
|
|
18
|
+
backtick: ["`", "`"],
|
|
19
|
+
parentheses: [`(`, `)`],
|
|
20
|
+
curlyBrackets: [`{`, `}`],
|
|
21
|
+
curlyDoubleInward: [`”`, `“`],
|
|
22
|
+
curlyDouble: [`“`, `”`],
|
|
23
|
+
curlyDoubleEqual: [`“`, `“`],
|
|
24
|
+
curlyLHDouble: [`„`, `”`],
|
|
25
|
+
curlyLHDoubleInward: [`„`, `“`],
|
|
26
|
+
curlyLHSingle: [`‚`, `’`],
|
|
27
|
+
curlyLHSingleInward: [`‚`, `‘`],
|
|
28
|
+
curlySingle: [`‛`, `’`],
|
|
29
|
+
curlySingleEqual: [`‛`, `‛`],
|
|
30
|
+
curlySingleInward: [`’`, `‛`],
|
|
31
|
+
double: [`"`, `"`],
|
|
32
|
+
guillemets: [`«`, `»`],
|
|
33
|
+
guillemetsInward: [`»`, `«`],
|
|
34
|
+
guillemetsSingle: [`‹`, `›`],
|
|
35
|
+
guillemetsSingleInward: [`›`, `‹`],
|
|
36
|
+
single: [`'`, `'`],
|
|
37
|
+
squareBrackets: [`[`, `]`],
|
|
38
|
+
};
|
|
39
|
+
quots.re = escapeRE([...new Set(
|
|
40
|
+
Object.values(quots)
|
|
41
|
+
.filter(v => Array.isArray(v))
|
|
42
|
+
.flat())].join(``), "g");
|
|
43
|
+
return quots;
|
|
37
44
|
}
|
|
38
45
|
|
|
39
|
-
function
|
|
40
|
-
return
|
|
46
|
+
function escapeRE(reString, modifiers) {
|
|
47
|
+
return new RegExp(reString.replace(/\p{S}|\p{P}/gu, a => `\\${a}`), modifiers);
|
|
41
48
|
}
|
|
42
49
|
|
|
43
|
-
function clone(instance) {
|
|
44
|
-
const newInstance = CustomStringConstructor(instance.value);
|
|
45
|
-
newInstance.history = [...instance.history];
|
|
46
|
-
return newInstance;
|
|
47
|
-
}
|
|
48
50
|
|
|
49
51
|
function getStringValue(string) {
|
|
50
52
|
return string?.value || (string?.constructor === String && string) || ``;
|
|
51
53
|
}
|
|
52
54
|
|
|
53
|
-
function
|
|
54
|
-
return value
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
function escapeRE(reString, modifiers) {
|
|
58
|
-
return new RegExp(reString.replace(/\p{S}|\p{P}/gu, a => `\\${a}`), modifiers);
|
|
55
|
+
function isArrayOf(type, value, includeInstances = true) {
|
|
56
|
+
return Array.isArray(value) && value.length > 0 &&
|
|
57
|
+
!value.find(v => checkType(type, v, includeInstances));
|
|
59
58
|
}
|
|
60
59
|
|
|
61
|
-
function
|
|
62
|
-
return
|
|
60
|
+
function isNumber(value) {
|
|
61
|
+
return value?.constructor === Number && !Number.isNaN(value);
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
function infoValue(key, infoValue) {
|
|
66
65
|
return `${key} (${infoValue})`;
|
|
67
66
|
}
|
|
68
67
|
|
|
69
|
-
function
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
clone: `chainable getter`,
|
|
74
|
-
notEmpty: `chainable getter|undefined`,
|
|
75
|
-
quote: `Object. See [constructor].quoteInfo`,
|
|
76
|
-
capitalize: `getter. Object with chainable getters: [${capitalizerKeys.join(`, `)}]`,
|
|
77
|
-
};
|
|
68
|
+
function checkType(type, item, includeInstances) {
|
|
69
|
+
return type === String && includeInstances
|
|
70
|
+
? item?.constructor !== CustomStringConstructor && item?.constructor !== type
|
|
71
|
+
: item?.constructor !== type;
|
|
78
72
|
}
|
|
79
73
|
|
|
80
|
-
function
|
|
81
|
-
|
|
82
|
-
|
|
74
|
+
function resolveTemplateString(str, ...args) {
|
|
75
|
+
return str?.raw
|
|
76
|
+
? String.raw({ raw: str }, ...args)
|
|
77
|
+
: getStringValue(str).length ? str : "";
|
|
78
|
+
}
|
|
83
79
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const custom = key in customMethods ? ` *custom*` : ``;
|
|
95
|
-
const getter = isGetter && isChainable ? `chainable getter${custom}` : `getter`;
|
|
96
|
-
const method = isMethod && isChainable ? `chainable method${custom}` : `method`;
|
|
97
|
-
const native = isNative && `${descriptr.get ? `getter` : `method`} (override)`;
|
|
98
|
-
|
|
99
|
-
switch (true) {
|
|
100
|
-
case isPlainValue: return infoValue(key, plainValues[key]);
|
|
101
|
-
case isNative: return infoValue(key, native);
|
|
102
|
-
case isMethod: return infoValue(key, method);
|
|
103
|
-
case isGetter: return infoValue(key, getter);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
)
|
|
107
|
-
);
|
|
80
|
+
function retrieveQuotInfo(instanceQuotGetters4Info, ctor) {
|
|
81
|
+
return Object.entries(Object.getOwnPropertyDescriptors(instanceQuotGetters4Info.value))
|
|
82
|
+
.sort( (a,b) => a[0].localeCompare(b[0]) )
|
|
83
|
+
.reduce((acc, [k,]) => {
|
|
84
|
+
if (k === `remove`) { return [...acc, `[instance].quote.remove (only predefined)`]; }
|
|
85
|
+
if (k === `custom`) { return [...acc, `[instance].quote.custom(start:string, end:string)`]; }
|
|
86
|
+
|
|
87
|
+
const val = ctor(`[instance]`).quote[k];
|
|
88
|
+
return [...acc, `[instance].quote.${k} ( ${val} )`];
|
|
89
|
+
}, []);
|
|
108
90
|
}
|
|
109
91
|
|
|
110
92
|
function quotGetters4Instance(instance, wrap) {
|
|
@@ -138,51 +120,48 @@ function quotGetters4Instance(instance, wrap) {
|
|
|
138
120
|
};
|
|
139
121
|
}
|
|
140
122
|
|
|
141
|
-
function
|
|
142
|
-
return
|
|
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
|
-
const regExpValues = escape4RE([...new Set(
|
|
170
|
-
Object.values(quots)
|
|
171
|
-
.filter(v => Array.isArray(v))
|
|
172
|
-
.flat())].join(``));
|
|
173
|
-
quots.re = RegExp(`^[${regExpValues}]|[${regExpValues}]$`, "g");
|
|
174
|
-
|
|
175
|
-
return quots;
|
|
123
|
+
function capitalizerFactory(instance, wrap) {
|
|
124
|
+
return {
|
|
125
|
+
get full() {
|
|
126
|
+
return wrap(instance.value.toUpperCase());
|
|
127
|
+
},
|
|
128
|
+
get none() {
|
|
129
|
+
return wrap(instance.value.toLowerCase());
|
|
130
|
+
},
|
|
131
|
+
get camel() {
|
|
132
|
+
return wrap(parseCamelcase(instance.value));
|
|
133
|
+
},
|
|
134
|
+
get snake() {
|
|
135
|
+
return wrap(parseSnakeCase(instance.value));
|
|
136
|
+
},
|
|
137
|
+
get first() {
|
|
138
|
+
return wrap(ucFirst(instance.value));
|
|
139
|
+
},
|
|
140
|
+
get kebab() {
|
|
141
|
+
return wrap(parseKebabCase(instance.value));
|
|
142
|
+
},
|
|
143
|
+
get words() {
|
|
144
|
+
return wrap(wordsFirstUp(instance.value));
|
|
145
|
+
},
|
|
146
|
+
get dashed() {
|
|
147
|
+
return wrap(parseKebabCase(instance.value));
|
|
148
|
+
},
|
|
149
|
+
}
|
|
176
150
|
}
|
|
177
151
|
|
|
178
152
|
function createExtendedCTOR(ctor, customMethods) {
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
153
|
+
const quoteInfo = retrieveQuotInfo(quotGetters4Instance(ctor()), ctor);
|
|
154
|
+
const swInfo = getSWInformation(`constructor,history,indexOf,toString,value,valueOf,empty`.split(`,`), ctor);
|
|
155
|
+
Symbol.toSB = Symbol(`toStringBuilder`);
|
|
156
|
+
Object.defineProperty(
|
|
157
|
+
String.prototype,
|
|
158
|
+
Symbol.toSB, {
|
|
159
|
+
get() { return ctor(this); },
|
|
160
|
+
enumerable: false,
|
|
161
|
+
configurable: false
|
|
162
|
+
}
|
|
163
|
+
);
|
|
164
|
+
|
|
186
165
|
Object.defineProperties(ctor, {
|
|
187
166
|
create: {
|
|
188
167
|
get() { return ctor(); },
|
|
@@ -210,9 +189,7 @@ function createExtendedCTOR(ctor, customMethods) {
|
|
|
210
189
|
}
|
|
211
190
|
}
|
|
212
191
|
},
|
|
213
|
-
info: {
|
|
214
|
-
get() { return getSWInformation(notChainable); }
|
|
215
|
-
},
|
|
192
|
+
info: { value: swInfo, },
|
|
216
193
|
keys: {
|
|
217
194
|
get() {
|
|
218
195
|
return Object.keys(Object.getOwnPropertyDescriptors(CustomStringConstructor``))
|
|
@@ -220,22 +197,8 @@ function createExtendedCTOR(ctor, customMethods) {
|
|
|
220
197
|
.map(v => !/constructor|toString|valueOf/.test(v) && v in customMethods ? `${v} *custom*` : v);
|
|
221
198
|
}
|
|
222
199
|
},
|
|
223
|
-
quoteInfo: {
|
|
224
|
-
|
|
225
|
-
return Object.entries(Object.getOwnPropertyDescriptors(instanceQuotGetters4Info.value))
|
|
226
|
-
.sort( (a,b) => a[0].localeCompare(b[0]) )
|
|
227
|
-
.reduce((acc, [k,]) => {
|
|
228
|
-
if (k === `remove`) { return [...acc, `[instance].quote.remove (only predefined)`]; }
|
|
229
|
-
if (k === `custom`) { return [...acc, `[instance].quote.custom(start:string, end:string)`]; }
|
|
230
|
-
|
|
231
|
-
const val = ctor(`[instance]`).quote[k];
|
|
232
|
-
return [...acc, `[instance].quote.${k} ( ${val} )`];
|
|
233
|
-
}, []);
|
|
234
|
-
}
|
|
235
|
-
},
|
|
236
|
-
uuid4: {
|
|
237
|
-
get() { return CustomStringConstructor(uuid4()); }
|
|
238
|
-
},
|
|
200
|
+
quoteInfo: { value: quoteInfo },
|
|
201
|
+
uuid4: { get() { return CustomStringConstructor(uuid4()); } },
|
|
239
202
|
randomString: {
|
|
240
203
|
value: function({len, includeUppercase, includeNumbers, includeSymbols, startAlphabetic} = {}) {
|
|
241
204
|
return CustomStringConstructor(randomString({len, includeUppercase, includeNumbers, includeSymbols, startAlphabetic}));
|
|
@@ -244,11 +207,54 @@ function createExtendedCTOR(ctor, customMethods) {
|
|
|
244
207
|
regExp: { value: createRegExp }
|
|
245
208
|
});
|
|
246
209
|
|
|
247
|
-
return
|
|
210
|
+
return;
|
|
248
211
|
}
|
|
249
212
|
|
|
250
|
-
function
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
213
|
+
function getSWInformation(notChainable, customMethods) {
|
|
214
|
+
const firstLines = CustomStringConstructor(getInfoPrefix());
|
|
215
|
+
const plainValues = getPlainValues();
|
|
216
|
+
|
|
217
|
+
return firstLines.split(/\n/)
|
|
218
|
+
.concat(
|
|
219
|
+
Object.entries(Object.getOwnPropertyDescriptors(firstLines))
|
|
220
|
+
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
|
|
221
|
+
.map(([key, descriptr]) => {
|
|
222
|
+
const isChainable = !notChainable.find(k => k === key);
|
|
223
|
+
const isGetter = 'get' in descriptr;
|
|
224
|
+
const isMethod = 'value' in descriptr;
|
|
225
|
+
const isNative = key in String.prototype;
|
|
226
|
+
const isPlainValue = !isNative && key in plainValues;
|
|
227
|
+
const custom = key in customMethods ? ` *custom*` : ``;
|
|
228
|
+
const getter = isGetter && isChainable ? `chainable getter${custom}` : `getter`;
|
|
229
|
+
const method = isMethod && isChainable ? `chainable method${custom}` : `method`;
|
|
230
|
+
const native = isNative && `${descriptr.get ? `getter` : `method`} (override)`;
|
|
231
|
+
|
|
232
|
+
switch (true) {
|
|
233
|
+
case isPlainValue: return infoValue(key, plainValues[key]);
|
|
234
|
+
case isNative: return infoValue(key, native);
|
|
235
|
+
case isMethod: return infoValue(key, method);
|
|
236
|
+
case isGetter: return infoValue(key, getter);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
)
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function getPlainValues() {
|
|
244
|
+
const capitalizerKeys = Object.keys(capitalizerFactory());
|
|
245
|
+
return {
|
|
246
|
+
value: `getter/setter`,
|
|
247
|
+
clone: `chainable getter`,
|
|
248
|
+
notEmpty: `chainable getter|undefined`,
|
|
249
|
+
quote: `Object. See [constructor].quoteInfo`,
|
|
250
|
+
capitalize: `getter. Object with chainable getters: [${capitalizerKeys.join(`, `)}]`,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function getInfoPrefix() {
|
|
255
|
+
return atob(
|
|
256
|
+
`Rm9yIHRoZSByZWNvcmQ6CltjbV0gY2hhaW5hYmxlIGdldHRlcnMvbWV0aG9kcyBtb2RpZnkgdGhlIGluc3RhbmNlIHN0cmluZwpbY21d`+
|
|
257
|
+
`IGluZGV4T2Ygb3ZlcnJpZGVzIHJldHVybnMgW3VuZGVmaW5lZF0gaWYgbm90aGluZyB3YXMgZm91bmQgKHNvIG9uZSBjYW4gdXNlIFtsYXN0`+
|
|
258
|
+
`SV1pbmRleE9mKFtzb21lIHN0cmluZyB2YWx1ZV0pID8/IDAKW2NtXSBpbmNsdWRlcyBpbmZvcm1hdGlvbiBmb3IgY3VzdG9tIG1ldGhv`+
|
|
259
|
+
`ZHMvZ2V0dGVycyBpZiBhcHBsaWNhYmxl`).replace(/\[cm]/g, `\u2714`);
|
|
254
260
|
}
|
|
@@ -20,12 +20,11 @@ import {
|
|
|
20
20
|
isNumber,
|
|
21
21
|
clone,
|
|
22
22
|
trim,
|
|
23
|
-
capitalizerFactory,
|
|
24
23
|
} from "./instanceMethods.js";
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
import { capitalizerFactory, deprecatedRE } from "./helpers.js";
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
export default instanceCreator;
|
|
29
28
|
|
|
30
29
|
function instanceCreator({initialstring} = {}) {
|
|
31
30
|
let customStringExtensions = { };
|
package/src/instanceMethods.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
+
import { customMethods, clone, } from "./CTORCreator.js";
|
|
2
|
+
|
|
1
3
|
import {
|
|
2
|
-
isArrayOf,
|
|
3
|
-
isNumber,
|
|
4
|
-
|
|
5
|
-
getStringValue,
|
|
6
|
-
escapeRE,
|
|
7
|
-
customMethods,
|
|
8
|
-
interpolate,
|
|
9
|
-
createRegExp as $RE,
|
|
10
|
-
clone } from "./genericMethods.js";
|
|
4
|
+
createRegExp as $RE, getStringValue, isArrayOf, interpolate,
|
|
5
|
+
isNumber, escapeRE, quotGetters4Instance as quotGetters,
|
|
6
|
+
} from "./helpers.js";
|
|
11
7
|
|
|
12
8
|
export {
|
|
13
9
|
format,
|
|
@@ -30,7 +26,6 @@ export {
|
|
|
30
26
|
surroundWith,
|
|
31
27
|
customMethods,
|
|
32
28
|
clone,
|
|
33
|
-
capitalizerFactory,
|
|
34
29
|
trim,
|
|
35
30
|
};
|
|
36
31
|
|
|
@@ -215,32 +210,3 @@ function append(string, ...strings2Append) {
|
|
|
215
210
|
|
|
216
211
|
return getStringValue(string);
|
|
217
212
|
}
|
|
218
|
-
|
|
219
|
-
function capitalizerFactory(instance, wrap) {
|
|
220
|
-
return {
|
|
221
|
-
get full() {
|
|
222
|
-
return wrap(instance.value.toUpperCase());
|
|
223
|
-
},
|
|
224
|
-
get none() {
|
|
225
|
-
return wrap(instance.value.toLowerCase());
|
|
226
|
-
},
|
|
227
|
-
get camel() {
|
|
228
|
-
return wrap(parseCamelcase(instance.value));
|
|
229
|
-
},
|
|
230
|
-
get snake() {
|
|
231
|
-
return wrap(parseSnakeCase(instance.value));
|
|
232
|
-
},
|
|
233
|
-
get first() {
|
|
234
|
-
return wrap(ucFirst(instance.value));
|
|
235
|
-
},
|
|
236
|
-
get kebab() {
|
|
237
|
-
return wrap(parseKebabCase(instance.value));
|
|
238
|
-
},
|
|
239
|
-
get words() {
|
|
240
|
-
return wrap(wordsFirstUp(instance.value));
|
|
241
|
-
},
|
|
242
|
-
get dashed() {
|
|
243
|
-
return wrap(parseKebabCase(instance.value));
|
|
244
|
-
},
|
|
245
|
-
}
|
|
246
|
-
}
|