@warp-drive-mirror/utilities 5.8.0-alpha.30 → 5.8.0-alpha.34
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/dist/string.js +428 -1
- package/dist/unpkg/dev/-private.js +7 -0
- package/dist/unpkg/dev/active-record.js +395 -0
- package/dist/unpkg/dev/builder-utils-Donkk-BZ.js +22 -0
- package/dist/unpkg/dev/declarations/-private/active-record/find-record.d.ts +64 -0
- package/dist/unpkg/dev/declarations/-private/active-record/query.d.ts +53 -0
- package/dist/unpkg/dev/declarations/-private/active-record/save-record.d.ts +146 -0
- package/dist/unpkg/dev/declarations/-private/builder-utils.d.ts +4 -0
- package/dist/unpkg/dev/declarations/-private/handlers/auto-compress.d.ts +158 -0
- package/dist/unpkg/dev/declarations/-private/handlers/gated.d.ts +19 -0
- package/dist/unpkg/dev/declarations/-private/handlers/meta-doc.d.ts +47 -0
- package/dist/unpkg/dev/declarations/-private/handlers/utils.d.ts +41 -0
- package/dist/unpkg/dev/declarations/-private/json-api/-utils.d.ts +109 -0
- package/dist/unpkg/dev/declarations/-private/json-api/find-record.d.ts +84 -0
- package/dist/unpkg/dev/declarations/-private/json-api/query.d.ts +100 -0
- package/dist/unpkg/dev/declarations/-private/json-api/save-record.d.ts +228 -0
- package/dist/unpkg/dev/declarations/-private/json-api/serialize.d.ts +70 -0
- package/dist/unpkg/dev/declarations/-private/rest/find-record.d.ts +62 -0
- package/dist/unpkg/dev/declarations/-private/rest/query.d.ts +53 -0
- package/dist/unpkg/dev/declarations/-private/rest/save-record.d.ts +146 -0
- package/dist/unpkg/dev/declarations/-private/string/inflect.d.ts +82 -0
- package/dist/unpkg/dev/declarations/-private/string/inflections.d.ts +9 -0
- package/dist/unpkg/dev/declarations/-private/string/transform.d.ts +77 -0
- package/dist/unpkg/dev/declarations/-private.d.ts +1 -0
- package/dist/unpkg/dev/declarations/active-record.d.ts +3 -0
- package/dist/unpkg/dev/declarations/derivations.d.ts +27 -0
- package/dist/unpkg/dev/declarations/handlers.d.ts +10 -0
- package/dist/unpkg/dev/declarations/index.d.ts +208 -0
- package/dist/unpkg/dev/declarations/json-api.d.ts +5 -0
- package/dist/unpkg/dev/declarations/rest.d.ts +3 -0
- package/dist/unpkg/dev/declarations/string.d.ts +14 -0
- package/dist/unpkg/dev/derivations.js +30 -0
- package/dist/unpkg/dev/handlers.js +317 -0
- package/dist/unpkg/dev/index.js +361 -0
- package/dist/unpkg/dev/inflect-BSG696t-.js +344 -0
- package/dist/unpkg/dev/json-api.js +740 -0
- package/dist/unpkg/dev/rest.js +393 -0
- package/dist/unpkg/dev/string.js +1 -0
- package/dist/unpkg/dev-deprecated/-private.js +7 -0
- package/dist/unpkg/dev-deprecated/active-record.js +395 -0
- package/dist/unpkg/dev-deprecated/builder-utils-Donkk-BZ.js +22 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/active-record/find-record.d.ts +64 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/active-record/query.d.ts +53 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/active-record/save-record.d.ts +146 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/builder-utils.d.ts +4 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/handlers/auto-compress.d.ts +158 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/handlers/gated.d.ts +19 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/handlers/meta-doc.d.ts +47 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/handlers/utils.d.ts +41 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/json-api/-utils.d.ts +109 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/json-api/find-record.d.ts +84 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/json-api/query.d.ts +100 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/json-api/save-record.d.ts +228 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/json-api/serialize.d.ts +70 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/rest/find-record.d.ts +62 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/rest/query.d.ts +53 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/rest/save-record.d.ts +146 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/string/inflect.d.ts +82 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/string/inflections.d.ts +9 -0
- package/dist/unpkg/dev-deprecated/declarations/-private/string/transform.d.ts +77 -0
- package/dist/unpkg/dev-deprecated/declarations/-private.d.ts +1 -0
- package/dist/unpkg/dev-deprecated/declarations/active-record.d.ts +3 -0
- package/dist/unpkg/dev-deprecated/declarations/derivations.d.ts +27 -0
- package/dist/unpkg/dev-deprecated/declarations/handlers.d.ts +10 -0
- package/dist/unpkg/dev-deprecated/declarations/index.d.ts +208 -0
- package/dist/unpkg/dev-deprecated/declarations/json-api.d.ts +5 -0
- package/dist/unpkg/dev-deprecated/declarations/rest.d.ts +3 -0
- package/dist/unpkg/dev-deprecated/declarations/string.d.ts +14 -0
- package/dist/unpkg/dev-deprecated/derivations.js +30 -0
- package/dist/unpkg/dev-deprecated/handlers.js +317 -0
- package/dist/unpkg/dev-deprecated/index.js +361 -0
- package/dist/unpkg/dev-deprecated/inflect-BSG696t-.js +344 -0
- package/dist/unpkg/dev-deprecated/json-api.js +740 -0
- package/dist/unpkg/dev-deprecated/rest.js +393 -0
- package/dist/unpkg/dev-deprecated/string.js +1 -0
- package/dist/unpkg/prod/-private.js +7 -0
- package/dist/unpkg/prod/active-record.js +395 -0
- package/dist/unpkg/prod/builder-utils-Donkk-BZ.js +22 -0
- package/dist/unpkg/prod/declarations/-private/active-record/find-record.d.ts +64 -0
- package/dist/unpkg/prod/declarations/-private/active-record/query.d.ts +53 -0
- package/dist/unpkg/prod/declarations/-private/active-record/save-record.d.ts +146 -0
- package/dist/unpkg/prod/declarations/-private/builder-utils.d.ts +4 -0
- package/dist/unpkg/prod/declarations/-private/handlers/auto-compress.d.ts +158 -0
- package/dist/unpkg/prod/declarations/-private/handlers/gated.d.ts +19 -0
- package/dist/unpkg/prod/declarations/-private/handlers/meta-doc.d.ts +47 -0
- package/dist/unpkg/prod/declarations/-private/handlers/utils.d.ts +41 -0
- package/dist/unpkg/prod/declarations/-private/json-api/-utils.d.ts +109 -0
- package/dist/unpkg/prod/declarations/-private/json-api/find-record.d.ts +84 -0
- package/dist/unpkg/prod/declarations/-private/json-api/query.d.ts +100 -0
- package/dist/unpkg/prod/declarations/-private/json-api/save-record.d.ts +228 -0
- package/dist/unpkg/prod/declarations/-private/json-api/serialize.d.ts +70 -0
- package/dist/unpkg/prod/declarations/-private/rest/find-record.d.ts +62 -0
- package/dist/unpkg/prod/declarations/-private/rest/query.d.ts +53 -0
- package/dist/unpkg/prod/declarations/-private/rest/save-record.d.ts +146 -0
- package/dist/unpkg/prod/declarations/-private/string/inflect.d.ts +82 -0
- package/dist/unpkg/prod/declarations/-private/string/inflections.d.ts +9 -0
- package/dist/unpkg/prod/declarations/-private/string/transform.d.ts +77 -0
- package/dist/unpkg/prod/declarations/-private.d.ts +1 -0
- package/dist/unpkg/prod/declarations/active-record.d.ts +3 -0
- package/dist/unpkg/prod/declarations/derivations.d.ts +27 -0
- package/dist/unpkg/prod/declarations/handlers.d.ts +10 -0
- package/dist/unpkg/prod/declarations/index.d.ts +208 -0
- package/dist/unpkg/prod/declarations/json-api.d.ts +5 -0
- package/dist/unpkg/prod/declarations/rest.d.ts +3 -0
- package/dist/unpkg/prod/declarations/string.d.ts +14 -0
- package/dist/unpkg/prod/derivations.js +30 -0
- package/dist/unpkg/prod/handlers.js +317 -0
- package/dist/unpkg/prod/index.js +361 -0
- package/dist/unpkg/prod/inflect-BSG696t-.js +344 -0
- package/dist/unpkg/prod/json-api.js +740 -0
- package/dist/unpkg/prod/rest.js +393 -0
- package/dist/unpkg/prod/string.js +1 -0
- package/dist/unpkg/prod-deprecated/-private.js +7 -0
- package/dist/unpkg/prod-deprecated/active-record.js +395 -0
- package/dist/unpkg/prod-deprecated/builder-utils-Donkk-BZ.js +22 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/active-record/find-record.d.ts +64 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/active-record/query.d.ts +53 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/active-record/save-record.d.ts +146 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/builder-utils.d.ts +4 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/handlers/auto-compress.d.ts +158 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/handlers/gated.d.ts +19 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/handlers/meta-doc.d.ts +47 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/handlers/utils.d.ts +41 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/json-api/-utils.d.ts +109 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/json-api/find-record.d.ts +84 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/json-api/query.d.ts +100 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/json-api/save-record.d.ts +228 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/json-api/serialize.d.ts +70 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/rest/find-record.d.ts +62 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/rest/query.d.ts +53 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/rest/save-record.d.ts +146 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/string/inflect.d.ts +82 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/string/inflections.d.ts +9 -0
- package/dist/unpkg/prod-deprecated/declarations/-private/string/transform.d.ts +77 -0
- package/dist/unpkg/prod-deprecated/declarations/-private.d.ts +1 -0
- package/dist/unpkg/prod-deprecated/declarations/active-record.d.ts +3 -0
- package/dist/unpkg/prod-deprecated/declarations/derivations.d.ts +27 -0
- package/dist/unpkg/prod-deprecated/declarations/handlers.d.ts +10 -0
- package/dist/unpkg/prod-deprecated/declarations/index.d.ts +208 -0
- package/dist/unpkg/prod-deprecated/declarations/json-api.d.ts +5 -0
- package/dist/unpkg/prod-deprecated/declarations/rest.d.ts +3 -0
- package/dist/unpkg/prod-deprecated/declarations/string.d.ts +14 -0
- package/dist/unpkg/prod-deprecated/derivations.js +30 -0
- package/dist/unpkg/prod-deprecated/handlers.js +317 -0
- package/dist/unpkg/prod-deprecated/index.js +361 -0
- package/dist/unpkg/prod-deprecated/inflect-BSG696t-.js +344 -0
- package/dist/unpkg/prod-deprecated/json-api.js +740 -0
- package/dist/unpkg/prod-deprecated/rest.js +393 -0
- package/dist/unpkg/prod-deprecated/string.js +1 -0
- package/package.json +28 -4
package/dist/string.js
CHANGED
|
@@ -1 +1,428 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, {
|
|
4
|
+
value: 'Module'
|
|
5
|
+
});
|
|
6
|
+
const DEFAULT_MAX_CACHE_SIZE = 10_000;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* An LRUCache implementation with upsert semantics.
|
|
10
|
+
*
|
|
11
|
+
* This implementation is *not* generic, but focuses on
|
|
12
|
+
* performance tuning for the string transformation cases
|
|
13
|
+
* where the key maps to the value very simply.
|
|
14
|
+
*
|
|
15
|
+
* It takes a work function that should generate a new value
|
|
16
|
+
* for a given key when called. It will be called when the key
|
|
17
|
+
* is not found in the cache.
|
|
18
|
+
*
|
|
19
|
+
* It keeps track of the number of hits, misses, and ejections
|
|
20
|
+
* in DEBUG envs, which is useful for tuning the cache size.
|
|
21
|
+
*
|
|
22
|
+
* This is an internal utility class for use by this module
|
|
23
|
+
* and by `@warp-drive-mirror/utilities/string`. It is not intended
|
|
24
|
+
* for use outside of these modules at this time.
|
|
25
|
+
*
|
|
26
|
+
* @private
|
|
27
|
+
*/
|
|
28
|
+
class LRUCache {
|
|
29
|
+
// debug stats
|
|
30
|
+
|
|
31
|
+
constructor(doWork, size) {
|
|
32
|
+
this.size = size || DEFAULT_MAX_CACHE_SIZE;
|
|
33
|
+
this.state = new Map();
|
|
34
|
+
this.doWork = doWork;
|
|
35
|
+
}
|
|
36
|
+
get(key) {
|
|
37
|
+
const value = this.state.get(key);
|
|
38
|
+
if (value) {
|
|
39
|
+
this.state.delete(key);
|
|
40
|
+
this.state.set(key, value);
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
const newValue = this.doWork(key);
|
|
44
|
+
this.set(key, newValue);
|
|
45
|
+
return newValue;
|
|
46
|
+
}
|
|
47
|
+
set(key, value) {
|
|
48
|
+
if (this.state.size === this.size) {
|
|
49
|
+
for (const [k] of this.state) {
|
|
50
|
+
this.state.delete(k);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.state.set(key, value);
|
|
55
|
+
}
|
|
56
|
+
clear() {
|
|
57
|
+
this.state.clear();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const STRING_DASHERIZE_REGEXP = /[ _]/g;
|
|
61
|
+
const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g;
|
|
62
|
+
const STRING_DASHERIZE_CACHE = new LRUCache(key => key.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase().replace(STRING_DASHERIZE_REGEXP, '-'));
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* This is an internal utility function that converts a string
|
|
66
|
+
* to a dasherized format. Library consumers should use the
|
|
67
|
+
* re-exported version from `@warp-drive-mirror/utilities/string` instead.
|
|
68
|
+
*
|
|
69
|
+
* This version is only in this location to support a deprecated
|
|
70
|
+
* behavior in the core package and will be removed in a future.
|
|
71
|
+
*
|
|
72
|
+
* @private
|
|
73
|
+
*/
|
|
74
|
+
function dasherize$1(str) {
|
|
75
|
+
return STRING_DASHERIZE_CACHE.get(str);
|
|
76
|
+
}
|
|
77
|
+
const defaultRules = {
|
|
78
|
+
plurals: [[/$/, 's'], [/s$/i, 's'], [/^(ax|test)is$/i, '$1es'], [/(octop|vir)us$/i, '$1i'], [/(octop|vir)i$/i, '$1i'], [/(alias|status|bonus)$/i, '$1es'], [/(bu)s$/i, '$1ses'], [/(buffal|tomat)o$/i, '$1oes'], [/([ti])um$/i, '$1a'], [/([ti])a$/i, '$1a'], [/sis$/i, 'ses'], [/(?:([^f])fe|([lr])f)$/i, '$1$2ves'], [/(hive)$/i, '$1s'], [/([^aeiouy]|qu)y$/i, '$1ies'], [/(x|ch|ss|sh)$/i, '$1es'], [/(matr|vert|ind)(?:ix|ex)$/i, '$1ices'], [/^(m|l)ouse$/i, '$1ice'], [/^(m|l)ice$/i, '$1ice'], [/^(ox)$/i, '$1en'], [/^(oxen)$/i, '$1'], [/(quiz)$/i, '$1zes']],
|
|
79
|
+
singular: [[/s$/i, ''], [/(ss)$/i, '$1'], [/(n)ews$/i, '$1ews'], [/([ti])a$/i, '$1um'], [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, '$1sis'], [/(^analy)(sis|ses)$/i, '$1sis'], [/([^f])ves$/i, '$1fe'], [/(hive)s$/i, '$1'], [/(tive)s$/i, '$1'], [/([lr])ves$/i, '$1f'], [/([^aeiouy]|qu)ies$/i, '$1y'], [/(s)eries$/i, '$1eries'], [/(m)ovies$/i, '$1ovie'], [/(x|ch|ss|sh)es$/i, '$1'], [/^(m|l)ice$/i, '$1ouse'], [/(bus)(es)?$/i, '$1'], [/(o)es$/i, '$1'], [/(shoe)s$/i, '$1'], [/(cris|test)(is|es)$/i, '$1is'], [/^(a)x[ie]s$/i, '$1xis'], [/(octop|vir)(us|i)$/i, '$1us'], [/(alias|status|bonus)(es)?$/i, '$1'], [/^(ox)en/i, '$1'], [/(vert|ind)ices$/i, '$1ex'], [/(matr)ices$/i, '$1ix'], [/(quiz)zes$/i, '$1'], [/(database)s$/i, '$1']],
|
|
80
|
+
irregularPairs: [['person', 'people'], ['man', 'men'], ['child', 'children'], ['sex', 'sexes'], ['move', 'moves'], ['cow', 'kine'], ['zombie', 'zombies']],
|
|
81
|
+
uncountable: ['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep', 'jeans', 'police']
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// eslint-disable-next-line no-useless-escape
|
|
85
|
+
const STRING_CAMELIZE_REGEXP_1 = /(\-|\_|\.|\s)+(.)?/g;
|
|
86
|
+
const STRING_CAMELIZE_REGEXP_2 = /(^|\/)([A-Z])/g;
|
|
87
|
+
const CAMELIZE_CACHE = new LRUCache(key => key.replace(STRING_CAMELIZE_REGEXP_1, (_match, _separator, chr) => chr ? chr.toUpperCase() : '').replace(STRING_CAMELIZE_REGEXP_2, (match /*, separator, chr */) => match.toLowerCase()));
|
|
88
|
+
const STRING_UNDERSCORE_REGEXP_1 = /([a-z\d])([A-Z]+)/g;
|
|
89
|
+
// eslint-disable-next-line no-useless-escape
|
|
90
|
+
const STRING_UNDERSCORE_REGEXP_2 = /\-|\s+/g;
|
|
91
|
+
const UNDERSCORE_CACHE = new LRUCache(str => str.replace(STRING_UNDERSCORE_REGEXP_1, '$1_$2').replace(STRING_UNDERSCORE_REGEXP_2, '_').toLowerCase());
|
|
92
|
+
const STRING_CAPITALIZE_REGEXP = /(^|\/)([a-z\u00C0-\u024F])/g;
|
|
93
|
+
const CAPITALIZE_CACHE = new LRUCache(str => str.replace(STRING_CAPITALIZE_REGEXP, (match /*, separator, chr */) => match.toUpperCase()));
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Replaces underscores, spaces, or camelCase with dashes.
|
|
97
|
+
*
|
|
98
|
+
* ```js
|
|
99
|
+
* import { dasherize } from '@warp-drive-mirror/utilities/string';
|
|
100
|
+
*
|
|
101
|
+
* dasherize('innerHTML'); // 'inner-html'
|
|
102
|
+
* dasherize('action_name'); // 'action-name'
|
|
103
|
+
* dasherize('css-class-name'); // 'css-class-name'
|
|
104
|
+
* dasherize('my favorite items'); // 'my-favorite-items'
|
|
105
|
+
* dasherize('privateDocs/ownerInvoice'; // 'private-docs/owner-invoice'
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @public
|
|
109
|
+
* @since 4.13.0
|
|
110
|
+
*/
|
|
111
|
+
const dasherize = dasherize$1;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Returns the lowerCamelCase form of a string.
|
|
115
|
+
*
|
|
116
|
+
* ```js
|
|
117
|
+
* import { camelize } from '@warp-drive-mirror/utilities/string';
|
|
118
|
+
*
|
|
119
|
+
* camelize('innerHTML'); // 'innerHTML'
|
|
120
|
+
* camelize('action_name'); // 'actionName'
|
|
121
|
+
* camelize('css-class-name'); // 'cssClassName'
|
|
122
|
+
* camelize('my favorite items'); // 'myFavoriteItems'
|
|
123
|
+
* camelize('My Favorite Items'); // 'myFavoriteItems'
|
|
124
|
+
* camelize('private-docs/owner-invoice'); // 'privateDocs/ownerInvoice'
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @public
|
|
128
|
+
* @since 4.13.0
|
|
129
|
+
*/
|
|
130
|
+
function camelize(str) {
|
|
131
|
+
return CAMELIZE_CACHE.get(str);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Returns the lower\_case\_and\_underscored form of a string.
|
|
136
|
+
*
|
|
137
|
+
* ```js
|
|
138
|
+
* import { underscore } from '@warp-drive-mirror/utilities/string';
|
|
139
|
+
*
|
|
140
|
+
* underscore('innerHTML'); // 'inner_html'
|
|
141
|
+
* underscore('action_name'); // 'action_name'
|
|
142
|
+
* underscore('css-class-name'); // 'css_class_name'
|
|
143
|
+
* underscore('my favorite items'); // 'my_favorite_items'
|
|
144
|
+
* underscore('privateDocs/ownerInvoice'); // 'private_docs/owner_invoice'
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @public
|
|
148
|
+
* @since 4.13.0
|
|
149
|
+
*/
|
|
150
|
+
function underscore(str) {
|
|
151
|
+
return UNDERSCORE_CACHE.get(str);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Returns the Capitalized form of a string
|
|
156
|
+
*
|
|
157
|
+
* ```js
|
|
158
|
+
* import { capitalize } from '@warp-drive-mirror/utilities/string';
|
|
159
|
+
*
|
|
160
|
+
* capitalize('innerHTML') // 'InnerHTML'
|
|
161
|
+
* capitalize('action_name') // 'Action_name'
|
|
162
|
+
* capitalize('css-class-name') // 'Css-class-name'
|
|
163
|
+
* capitalize('my favorite items') // 'My favorite items'
|
|
164
|
+
* capitalize('privateDocs/ownerInvoice'); // 'PrivateDocs/ownerInvoice'
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* @public
|
|
168
|
+
* @since 4.13.0
|
|
169
|
+
*/
|
|
170
|
+
function capitalize(str) {
|
|
171
|
+
return CAPITALIZE_CACHE.get(str);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Sets the maximum size of the LRUCache for all string transformation functions.
|
|
176
|
+
* The default size is 10,000.
|
|
177
|
+
*
|
|
178
|
+
* @public
|
|
179
|
+
* @since 4.13.0
|
|
180
|
+
*/
|
|
181
|
+
function setMaxLRUCacheSize(size) {
|
|
182
|
+
CAMELIZE_CACHE.size = size;
|
|
183
|
+
UNDERSCORE_CACHE.size = size;
|
|
184
|
+
CAPITALIZE_CACHE.size = size;
|
|
185
|
+
STRING_DASHERIZE_CACHE.size = size;
|
|
186
|
+
}
|
|
187
|
+
const BLANK_REGEX = /^\s*$/;
|
|
188
|
+
const LAST_WORD_DASHED_REGEX = /([\w/-]+[_/\s-])([a-z\d]+$)/;
|
|
189
|
+
const LAST_WORD_CAMELIZED_REGEX = /([\w/\s-]+)([A-Z][a-z\d]*$)/;
|
|
190
|
+
const CAMELIZED_REGEX = /[A-Z][a-z\d]*$/;
|
|
191
|
+
const SINGULARS = new LRUCache(word => {
|
|
192
|
+
return _singularize(word);
|
|
193
|
+
});
|
|
194
|
+
const PLURALS = new LRUCache(word => {
|
|
195
|
+
return _pluralize(word);
|
|
196
|
+
});
|
|
197
|
+
const UNCOUNTABLE = new Set(defaultRules.uncountable);
|
|
198
|
+
const IRREGULAR = new Map();
|
|
199
|
+
const INVERSE_IRREGULAR = new Map();
|
|
200
|
+
const SINGULAR_RULES = new Map(defaultRules.singular.reverse());
|
|
201
|
+
const PLURAL_RULES = new Map(defaultRules.plurals.reverse());
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Marks a word as uncountable. Uncountable words are not pluralized
|
|
205
|
+
* or singularized.
|
|
206
|
+
*
|
|
207
|
+
* @public
|
|
208
|
+
* @since 4.13.0
|
|
209
|
+
*/
|
|
210
|
+
function uncountable(word) {
|
|
211
|
+
UNCOUNTABLE.add(word.toLowerCase());
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Marks a list of words as uncountable. Uncountable words are not pluralized
|
|
216
|
+
* or singularized.
|
|
217
|
+
*
|
|
218
|
+
* @public
|
|
219
|
+
* @since 4.13.0
|
|
220
|
+
*/
|
|
221
|
+
function loadUncountable(uncountables) {
|
|
222
|
+
uncountables.forEach(word => {
|
|
223
|
+
uncountable(word);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Marks a word as irregular. Irregular words have unique
|
|
229
|
+
* pluralization and singularization rules.
|
|
230
|
+
*
|
|
231
|
+
* @public
|
|
232
|
+
* @since 4.13.0
|
|
233
|
+
*/
|
|
234
|
+
function irregular(single, plur) {
|
|
235
|
+
//pluralizing
|
|
236
|
+
IRREGULAR.set(single.toLowerCase(), plur);
|
|
237
|
+
IRREGULAR.set(plur.toLowerCase(), plur);
|
|
238
|
+
|
|
239
|
+
//singularizing
|
|
240
|
+
INVERSE_IRREGULAR.set(plur.toLowerCase(), single);
|
|
241
|
+
INVERSE_IRREGULAR.set(single.toLowerCase(), single);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Marks a list of word pairs as irregular. Irregular words have unique
|
|
246
|
+
* pluralization and singularization rules.
|
|
247
|
+
*
|
|
248
|
+
* @public
|
|
249
|
+
* @since 4.13.0
|
|
250
|
+
*/
|
|
251
|
+
function loadIrregular(irregularPairs) {
|
|
252
|
+
irregularPairs.forEach(pair => {
|
|
253
|
+
//pluralizing
|
|
254
|
+
IRREGULAR.set(pair[0].toLowerCase(), pair[1]);
|
|
255
|
+
IRREGULAR.set(pair[1].toLowerCase(), pair[1]);
|
|
256
|
+
|
|
257
|
+
//singularizing
|
|
258
|
+
INVERSE_IRREGULAR.set(pair[1].toLowerCase(), pair[0]);
|
|
259
|
+
INVERSE_IRREGULAR.set(pair[0].toLowerCase(), pair[0]);
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
loadIrregular(defaultRules.irregularPairs);
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Clears the caches for singularize and pluralize.
|
|
266
|
+
*
|
|
267
|
+
* @public
|
|
268
|
+
* @since 4.13.0
|
|
269
|
+
*/
|
|
270
|
+
function clear() {
|
|
271
|
+
SINGULARS.clear();
|
|
272
|
+
PLURALS.clear();
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Resets the inflection rules to the defaults.
|
|
277
|
+
*
|
|
278
|
+
* @public
|
|
279
|
+
* @since 4.13.0
|
|
280
|
+
*/
|
|
281
|
+
function resetToDefaults() {
|
|
282
|
+
clearRules();
|
|
283
|
+
defaultRules.uncountable.forEach(v => UNCOUNTABLE.add(v));
|
|
284
|
+
defaultRules.singular.forEach(v => SINGULAR_RULES.set(v[0], v[1]));
|
|
285
|
+
defaultRules.plurals.forEach(v => PLURAL_RULES.set(v[0], v[1]));
|
|
286
|
+
loadIrregular(defaultRules.irregularPairs);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Clears all inflection rules
|
|
291
|
+
* and resets the caches for singularize and pluralize.
|
|
292
|
+
*
|
|
293
|
+
* @public
|
|
294
|
+
* @since 4.13.0
|
|
295
|
+
*/
|
|
296
|
+
function clearRules() {
|
|
297
|
+
SINGULARS.clear();
|
|
298
|
+
PLURALS.clear();
|
|
299
|
+
UNCOUNTABLE.clear();
|
|
300
|
+
IRREGULAR.clear();
|
|
301
|
+
INVERSE_IRREGULAR.clear();
|
|
302
|
+
SINGULAR_RULES.clear();
|
|
303
|
+
PLURAL_RULES.clear();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Singularizes a word.
|
|
308
|
+
*
|
|
309
|
+
* @public
|
|
310
|
+
* @since 4.13.0
|
|
311
|
+
*/
|
|
312
|
+
function singularize(word) {
|
|
313
|
+
if (!word) return '';
|
|
314
|
+
return SINGULARS.get(word);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Pluralizes a word.
|
|
319
|
+
*
|
|
320
|
+
* @public
|
|
321
|
+
* @since 4.13.0
|
|
322
|
+
*/
|
|
323
|
+
function pluralize(word) {
|
|
324
|
+
if (!word) return '';
|
|
325
|
+
return PLURALS.get(word);
|
|
326
|
+
}
|
|
327
|
+
function unshiftMap(v, map) {
|
|
328
|
+
// reorder
|
|
329
|
+
const rules = [v, ...map.entries()];
|
|
330
|
+
map.clear();
|
|
331
|
+
rules.forEach(rule => {
|
|
332
|
+
map.set(rule[0], rule[1]);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Adds a pluralization rule.
|
|
338
|
+
*
|
|
339
|
+
* @public
|
|
340
|
+
* @since 4.13.0
|
|
341
|
+
*/
|
|
342
|
+
function plural(regex, string) {
|
|
343
|
+
// rule requires reordering if exists, so remove it first
|
|
344
|
+
if (PLURAL_RULES.has(regex)) {
|
|
345
|
+
PLURAL_RULES.delete(regex);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// reorder
|
|
349
|
+
unshiftMap([regex, string], PLURAL_RULES);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Adds a singularization rule.
|
|
354
|
+
*
|
|
355
|
+
* @public
|
|
356
|
+
* @since 4.13.0
|
|
357
|
+
*/
|
|
358
|
+
function singular(regex, string) {
|
|
359
|
+
// rule requires reordering if exists, so remove it first
|
|
360
|
+
if (SINGULAR_RULES.has(regex)) {
|
|
361
|
+
SINGULAR_RULES.delete(regex);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// reorder
|
|
365
|
+
unshiftMap([regex, string], SINGULAR_RULES);
|
|
366
|
+
}
|
|
367
|
+
function _pluralize(word) {
|
|
368
|
+
return inflect(word, PLURAL_RULES, IRREGULAR);
|
|
369
|
+
}
|
|
370
|
+
function _singularize(word) {
|
|
371
|
+
return inflect(word, SINGULAR_RULES, INVERSE_IRREGULAR);
|
|
372
|
+
}
|
|
373
|
+
function inflect(word, typeRules, irregulars) {
|
|
374
|
+
// empty strings
|
|
375
|
+
const isBlank = !word || BLANK_REGEX.test(word);
|
|
376
|
+
if (isBlank) {
|
|
377
|
+
return word;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// basic uncountables
|
|
381
|
+
const lowercase = word.toLowerCase();
|
|
382
|
+
if (UNCOUNTABLE.has(lowercase)) {
|
|
383
|
+
return word;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// adv uncountables
|
|
387
|
+
const wordSplit = LAST_WORD_DASHED_REGEX.exec(word) || LAST_WORD_CAMELIZED_REGEX.exec(word);
|
|
388
|
+
const lastWord = wordSplit ? wordSplit[2].toLowerCase() : null;
|
|
389
|
+
if (lastWord && UNCOUNTABLE.has(lastWord)) {
|
|
390
|
+
return word;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// handle irregulars
|
|
394
|
+
const isCamelized = CAMELIZED_REGEX.test(word);
|
|
395
|
+
for (let [rule, substitution] of irregulars) {
|
|
396
|
+
if (lowercase.match(rule + '$')) {
|
|
397
|
+
if (isCamelized && lastWord && irregulars.has(lastWord)) {
|
|
398
|
+
substitution = capitalize(substitution);
|
|
399
|
+
rule = capitalize(rule);
|
|
400
|
+
}
|
|
401
|
+
return word.replace(new RegExp(rule, 'i'), substitution);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// do the actual inflection
|
|
406
|
+
for (const [rule, substitution] of typeRules) {
|
|
407
|
+
if (rule.test(word)) {
|
|
408
|
+
return word.replace(rule, substitution);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return word;
|
|
412
|
+
}
|
|
413
|
+
exports.camelize = camelize;
|
|
414
|
+
exports.capitalize = capitalize;
|
|
415
|
+
exports.clear = clear;
|
|
416
|
+
exports.clearRules = clearRules;
|
|
417
|
+
exports.dasherize = dasherize;
|
|
418
|
+
exports.irregular = irregular;
|
|
419
|
+
exports.loadIrregular = loadIrregular;
|
|
420
|
+
exports.loadUncountable = loadUncountable;
|
|
421
|
+
exports.plural = plural;
|
|
422
|
+
exports.pluralize = pluralize;
|
|
423
|
+
exports.resetToDefaults = resetToDefaults;
|
|
424
|
+
exports.setMaxLRUCacheSize = setMaxLRUCacheSize;
|
|
425
|
+
exports.singular = singular;
|
|
426
|
+
exports.singularize = singularize;
|
|
427
|
+
exports.uncountable = uncountable;
|
|
428
|
+
exports.underscore = underscore;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const defaultRules = {
|
|
2
|
+
plurals: [[/$/, 's'], [/s$/i, 's'], [/^(ax|test)is$/i, '$1es'], [/(octop|vir)us$/i, '$1i'], [/(octop|vir)i$/i, '$1i'], [/(alias|status|bonus)$/i, '$1es'], [/(bu)s$/i, '$1ses'], [/(buffal|tomat)o$/i, '$1oes'], [/([ti])um$/i, '$1a'], [/([ti])a$/i, '$1a'], [/sis$/i, 'ses'], [/(?:([^f])fe|([lr])f)$/i, '$1$2ves'], [/(hive)$/i, '$1s'], [/([^aeiouy]|qu)y$/i, '$1ies'], [/(x|ch|ss|sh)$/i, '$1es'], [/(matr|vert|ind)(?:ix|ex)$/i, '$1ices'], [/^(m|l)ouse$/i, '$1ice'], [/^(m|l)ice$/i, '$1ice'], [/^(ox)$/i, '$1en'], [/^(oxen)$/i, '$1'], [/(quiz)$/i, '$1zes']],
|
|
3
|
+
singular: [[/s$/i, ''], [/(ss)$/i, '$1'], [/(n)ews$/i, '$1ews'], [/([ti])a$/i, '$1um'], [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$/i, '$1sis'], [/(^analy)(sis|ses)$/i, '$1sis'], [/([^f])ves$/i, '$1fe'], [/(hive)s$/i, '$1'], [/(tive)s$/i, '$1'], [/([lr])ves$/i, '$1f'], [/([^aeiouy]|qu)ies$/i, '$1y'], [/(s)eries$/i, '$1eries'], [/(m)ovies$/i, '$1ovie'], [/(x|ch|ss|sh)es$/i, '$1'], [/^(m|l)ice$/i, '$1ouse'], [/(bus)(es)?$/i, '$1'], [/(o)es$/i, '$1'], [/(shoe)s$/i, '$1'], [/(cris|test)(is|es)$/i, '$1is'], [/^(a)x[ie]s$/i, '$1xis'], [/(octop|vir)(us|i)$/i, '$1us'], [/(alias|status|bonus)(es)?$/i, '$1'], [/^(ox)en/i, '$1'], [/(vert|ind)ices$/i, '$1ex'], [/(matr)ices$/i, '$1ix'], [/(quiz)zes$/i, '$1'], [/(database)s$/i, '$1']],
|
|
4
|
+
irregularPairs: [['person', 'people'], ['man', 'men'], ['child', 'children'], ['sex', 'sexes'], ['move', 'moves'], ['cow', 'kine'], ['zombie', 'zombies']],
|
|
5
|
+
uncountable: ['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep', 'jeans', 'police']
|
|
6
|
+
};
|
|
7
|
+
export { defaultRules as InflectionRuleDefaults };
|