stringweaver 1.4.2 → 1.4.4

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/src/extensions.js DELETED
@@ -1,170 +0,0 @@
1
- import {
2
- format,
3
- ucFirst,
4
- truncate,
5
- trimAll,
6
- replaceWords,
7
- indexOf,
8
- lastIndexOf,
9
- insert,
10
- append,
11
- prefix,
12
- getStringValue,
13
- quotGetters,
14
- surroundWith,
15
- parseCamelcase,
16
- wordsFirstUp,
17
- parseKebabCase,
18
- parseSnakeCase,
19
- customMethods,
20
- isNumber,
21
- clone,
22
- trim,
23
- capitalizerFactory,
24
- } from "./instanceMethods.js";
25
-
26
- export default instanceCreator;
27
-
28
- const deprecatedRE = /symbol|anchor|big|blink|bold|fixed|fontsize|fontcolor|italics|link|small|strike|sup|sub/i
29
-
30
- function instanceCreator({initialstring} = {}) {
31
- let customStringExtensions = { };
32
- let instance = new Proxy(customStringExtensions, getTraps(customStringExtensions));
33
- let actualValue = getStringValue(initialstring);
34
- let history = [actualValue];
35
- const descriptorProps = {configurable: false, enumerable: false};
36
-
37
- Object.defineProperties( customStringExtensions, {
38
- // methods
39
- append: { ...descriptorProps, value(...strings) { return wrap(append(actualValue, ...strings)); } },
40
- enclose: { ...descriptorProps, value(start, end) { return wrap(surroundWith(actualValue, start, end)); } },
41
- format: { ...descriptorProps, value(...tokens) { return wrap(format(actualValue, ...tokens)); } },
42
- indexOf: { ...descriptorProps, value(str) { return indexOf(actualValue, str); } },
43
- interpolate: { ...descriptorProps, value(...tokens) { return wrap(format(actualValue, ...tokens)); } },
44
- insert: { ...descriptorProps, value({ value, values, at } = {}) {
45
- return wrap(insert(actualValue, { value, values, at }));
46
- }
47
- },
48
- lastIndexOf: { ...descriptorProps, value(str) { return lastIndexOf(actualValue, str); } },
49
- prefix: { ...descriptorProps, value(...strings) { return wrap(prefix(actualValue, ...strings)); } },
50
- replaceWords: { ...descriptorProps, value({caseSensitive = false, replacements = {}} = {}) {
51
- return wrap(replaceWords(actualValue, { replacements: replacements ?? {}, caseSensitive }));
52
- } },
53
- toString: { ...descriptorProps, value() { return actualValue; } },
54
- trim: {...descriptorProps, value(start, end) { return wrap(trim(actualValue, start, end)); } },
55
- truncate: { ...descriptorProps, value({at, html = false, wordBoundary = false} = {}) {
56
- return wrap(truncate(actualValue, {at, html, wordBoundary})); } },
57
- valueOf: { ...descriptorProps, value() { return actualValue; } },
58
- undoLast: { ...descriptorProps, value(nSteps) { return undoSteps(nSteps); } },
59
-
60
- // getters
61
- camelCase: { ...descriptorProps, get() { return wrap(parseCamelcase(getStringValue(actualValue))); } },
62
- capitalize: { ...descriptorProps, value: capitalizerFactory(instance, wrap) },
63
- clone: { ...descriptorProps, get() { return clone(instance, customMethods); } },
64
- firstUp: { ...descriptorProps, get() { return wrap(ucFirst(getStringValue(actualValue))); } },
65
- history: { ...descriptorProps, get() { return history; }, set(value) { history = value; } },
66
- empty: { ...descriptorProps, get() { return actualValue.length < 1; } },
67
- notEmpty: { ...descriptorProps, get() { return actualValue.length < 1 ? undefined: instance; } },
68
- kebabCase: { ...descriptorProps, get() { return wrap(parseKebabCase(getStringValue(actualValue))); } },
69
- quote: quotGetters(instance, wrap),
70
- snakeCase: { ...descriptorProps, get() { return wrap(parseSnakeCase(getStringValue(actualValue))); } },
71
- trimAll: { ...descriptorProps, get() { return wrap(trimAll(actualValue)); } },
72
- trimAllKeepLF: { ...descriptorProps, get() { return wrap(trimAll(actualValue, true)); } },
73
- undoAll: { ...descriptorProps, get() { return undoAll(); } },
74
- undo: { ...descriptorProps, get() { return undoLast(); } },
75
- wordsUCFirst: { ...descriptorProps, get() { return wrap(wordsFirstUp(getStringValue(actualValue))); } },
76
- value: { ...descriptorProps,
77
- get() { return actualValue; },
78
- set(value) {
79
- const nwValue = getStringValue(value);
80
- if (nwValue.length) {
81
- actualValue = nwValue;
82
- history.push(nwValue);
83
- }
84
- }
85
- },
86
- });
87
-
88
- injectCustomMethods(customMethods);
89
-
90
- return instance;
91
-
92
- function getTraps(extensions) {
93
- return {
94
- get( target, key ) {
95
- return Object.hasOwn(extensions, key)
96
- ? extensions[key]
97
- : canWrapNative(String(key))
98
- ? wrapNative(key)
99
- : undefined;
100
- },
101
- };
102
- }
103
-
104
- function canWrapNative(key) {
105
- return !deprecatedRE.test(key)
106
- && key in String.prototype;
107
- }
108
-
109
- function wrapNative(key) {
110
- return actualValue[key] instanceof Function
111
- ? function(...args) {
112
- const result = actualValue[key](...args);
113
- return result?.constructor === String ? wrap(actualValue[key](...args)) : result;
114
- } : actualValue[key];
115
- }
116
-
117
- function undoAll() {
118
- while (history.length > 1) { history.pop(); }
119
- actualValue = history.at(-1);
120
- return wrap(actualValue, false);
121
- }
122
-
123
- function undoSteps(steps) {
124
- if (!isNumber(steps)) {
125
- return wrap(actualValue, false);
126
- }
127
-
128
- const historyLen = history.length;
129
-
130
- if (steps >= historyLen || steps < 1) {
131
- history = history.slice(0, 1);
132
- actualValue = history.at(-1);
133
- return wrap(history.at(-1), false);
134
- }
135
-
136
- history = history.slice(0, historyLen - steps);
137
-
138
- actualValue = history.at(-1);
139
- return wrap(actualValue, false);
140
- }
141
-
142
- function undoLast() {
143
- if (history.length === 1) {
144
- return wrap(history[0]);
145
- }
146
-
147
- history.pop();
148
- actualValue = history.at(-1);
149
- return wrap(actualValue, false);
150
- }
151
-
152
- function wrap(result, pushHistory = true) {
153
- const changed = actualValue !== result;
154
- changed && pushHistory && history.push(result);
155
- actualValue = result;
156
- return instance;
157
- }
158
-
159
- function injectCustomMethods(customMethods) {
160
- Object.entries(customMethods).forEach(([methodName, methodContainer]) => {
161
- const {enumerable, method, isGetter} = methodContainer;
162
- const configurable = false
163
- const descriptor = isGetter
164
- ? { get() { return wrap(method(instance).value); }, enumerable, configurable }
165
- : { value(...args) { return wrap(method(instance, ...args).value); }, enumerable, configurable };
166
-
167
- Object.defineProperty(customStringExtensions, methodName, descriptor);
168
- });
169
- }
170
- }