@warp-drive-mirror/utilities 5.8.0-beta.0 → 5.8.0-beta.1
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 +16 -25
- package/declarations/-private/active-record/find-record.d.ts +5 -6
- package/declarations/-private/active-record/query.d.ts +2 -2
- package/declarations/-private/active-record/save-record.d.ts +2 -2
- package/declarations/-private/handlers/meta-doc.d.ts +1 -1
- package/declarations/-private/json-api/find-record.d.ts +37 -18
- package/declarations/-private/json-api/query.d.ts +3 -6
- package/declarations/-private/json-api/save-record.d.ts +45 -10
- package/declarations/-private/json-api/serialize.d.ts +15 -1
- package/declarations/-private/rest/find-record.d.ts +5 -8
- package/declarations/-private/rest/query.d.ts +2 -2
- package/declarations/-private/rest/save-record.d.ts +2 -2
- package/dist/active-record.js +2 -0
- package/dist/handlers.js +3 -3
- package/dist/json-api.js +94 -26
- package/dist/rest.js +2 -2
- package/dist/string.js +428 -1
- package/dist/unpkg/dev/-private.js +7 -0
- package/dist/unpkg/dev/active-record.js +394 -0
- package/dist/unpkg/dev/builder-utils-Donkk-BZ.js +22 -0
- package/dist/unpkg/dev/derivations.js +30 -0
- package/dist/unpkg/dev/handlers.js +316 -0
- package/dist/unpkg/dev/index.js +362 -0
- package/dist/unpkg/dev/inflect-BEv8WqY1.js +343 -0
- package/dist/unpkg/dev/json-api.js +740 -0
- package/dist/unpkg/dev/rest.js +392 -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 +394 -0
- package/dist/unpkg/dev-deprecated/builder-utils-Donkk-BZ.js +22 -0
- package/dist/unpkg/dev-deprecated/derivations.js +30 -0
- package/dist/unpkg/dev-deprecated/handlers.js +316 -0
- package/dist/unpkg/dev-deprecated/index.js +362 -0
- package/dist/unpkg/dev-deprecated/inflect-BEv8WqY1.js +343 -0
- package/dist/unpkg/dev-deprecated/json-api.js +740 -0
- package/dist/unpkg/dev-deprecated/rest.js +392 -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 +366 -0
- package/dist/unpkg/prod/builder-utils-Donkk-BZ.js +22 -0
- package/dist/unpkg/prod/derivations.js +30 -0
- package/dist/unpkg/prod/handlers.js +305 -0
- package/dist/unpkg/prod/index.js +239 -0
- package/dist/unpkg/prod/inflect-Dh9dyEYx.js +333 -0
- package/dist/unpkg/prod/json-api.js +702 -0
- package/dist/unpkg/prod/rest.js +364 -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 +366 -0
- package/dist/unpkg/prod-deprecated/builder-utils-Donkk-BZ.js +22 -0
- package/dist/unpkg/prod-deprecated/derivations.js +30 -0
- package/dist/unpkg/prod-deprecated/handlers.js +305 -0
- package/dist/unpkg/prod-deprecated/index.js +239 -0
- package/dist/unpkg/prod-deprecated/inflect-Dh9dyEYx.js +333 -0
- package/dist/unpkg/prod-deprecated/json-api.js +702 -0
- package/dist/unpkg/prod-deprecated/rest.js +364 -0
- package/dist/unpkg/prod-deprecated/string.js +1 -0
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +13 -5
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
package/dist/json-api.js
CHANGED
|
@@ -74,32 +74,49 @@ function setBuildURLConfig(config) {
|
|
|
74
74
|
* Builds request options to fetch a single resource by a known id or identifier
|
|
75
75
|
* configured for the url and header expectations of most JSON:API APIs.
|
|
76
76
|
*
|
|
77
|
-
*
|
|
77
|
+
* :::tabs
|
|
78
|
+
*
|
|
79
|
+
* == Basic Usage
|
|
78
80
|
*
|
|
79
81
|
* ```ts
|
|
80
82
|
* import { findRecord } from '@warp-drive-mirror/utilities/json-api';
|
|
83
|
+
* import type { Person } from '#/data/types';
|
|
81
84
|
*
|
|
82
|
-
* const
|
|
85
|
+
* const result = await store.request(
|
|
86
|
+
* findRecord<Person>('person', '1')
|
|
87
|
+
* );
|
|
83
88
|
* ```
|
|
84
89
|
*
|
|
85
|
-
*
|
|
90
|
+
* == With Options
|
|
86
91
|
*
|
|
87
92
|
* ```ts
|
|
88
93
|
* import { findRecord } from '@warp-drive-mirror/utilities/json-api';
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* const data = await store.request(
|
|
94
|
+
* import type { Person } from '#/data/types';
|
|
95
|
+
*
|
|
96
|
+
* const data = await store.request(
|
|
97
|
+
* findRecord<Person>(
|
|
98
|
+
* 'person', '1',
|
|
99
|
+
* { include: ['pets', 'friends'] }
|
|
100
|
+
* )
|
|
101
|
+
* );
|
|
92
102
|
* ```
|
|
93
103
|
*
|
|
94
|
-
*
|
|
104
|
+
* == With an Identifier
|
|
95
105
|
*
|
|
96
106
|
* ```ts
|
|
97
107
|
* import { findRecord } from '@warp-drive-mirror/utilities/json-api';
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
* const data = await store.request(
|
|
108
|
+
* import type { Person } from '#/data/types';
|
|
109
|
+
*
|
|
110
|
+
* const data = await store.request(
|
|
111
|
+
* findRecord<Person>(
|
|
112
|
+
* { type: 'person', id: '1' },
|
|
113
|
+
* { include: ['pets', 'friends'] }
|
|
114
|
+
* )
|
|
115
|
+
* );
|
|
101
116
|
* ```
|
|
102
117
|
*
|
|
118
|
+
* :::
|
|
119
|
+
*
|
|
103
120
|
* **Supplying Options to Modify the Request Behavior**
|
|
104
121
|
*
|
|
105
122
|
* The following options are supported:
|
|
@@ -117,13 +134,16 @@ function setBuildURLConfig(config) {
|
|
|
117
134
|
* ```ts
|
|
118
135
|
* import { findRecord } from '@warp-drive-mirror/utilities/json-api';
|
|
119
136
|
*
|
|
120
|
-
* const
|
|
121
|
-
*
|
|
137
|
+
* const data = await store.request(
|
|
138
|
+
* findRecord(
|
|
139
|
+
* 'person', '1',
|
|
140
|
+
* { include: ['pets', 'friends'] },
|
|
141
|
+
* { namespace: 'api/v2' }
|
|
142
|
+
* )
|
|
143
|
+
* );
|
|
122
144
|
* ```
|
|
123
145
|
*
|
|
124
146
|
* @public
|
|
125
|
-
* @param identifier
|
|
126
|
-
* @param options
|
|
127
147
|
*/
|
|
128
148
|
|
|
129
149
|
function findRecord(arg1, arg2, arg3) {
|
|
@@ -154,6 +174,8 @@ function findRecord(arg1, arg2, arg3) {
|
|
|
154
174
|
};
|
|
155
175
|
}
|
|
156
176
|
|
|
177
|
+
/** @deprecated use {@link ReactiveDataDocument} */
|
|
178
|
+
|
|
157
179
|
/**
|
|
158
180
|
* Builds request options to query for resources, usually by a primary
|
|
159
181
|
* type, configured for the url and header expectations of most JSON:API APIs.
|
|
@@ -201,9 +223,6 @@ function findRecord(arg1, arg2, arg3) {
|
|
|
201
223
|
* ```
|
|
202
224
|
*
|
|
203
225
|
* @public
|
|
204
|
-
* @param identifier
|
|
205
|
-
* @param query
|
|
206
|
-
* @param options
|
|
207
226
|
*/
|
|
208
227
|
|
|
209
228
|
function query(type,
|
|
@@ -307,6 +326,11 @@ function isExisting(identifier) {
|
|
|
307
326
|
}
|
|
308
327
|
|
|
309
328
|
/**
|
|
329
|
+
* :::warning ⚠️ **These Mutation Builders DO NOT Set The Request Body**
|
|
330
|
+
* While this may come as a surprise, the app providing the body ensures that only
|
|
331
|
+
* desired and correctly formatted data is sent with the request.
|
|
332
|
+
* :::
|
|
333
|
+
*
|
|
310
334
|
* Builds request options to delete record for resources,
|
|
311
335
|
* configured for the url, method and header expectations of most JSON:API APIs.
|
|
312
336
|
*
|
|
@@ -390,16 +414,31 @@ function deleteRecord(record, options = {}) {
|
|
|
390
414
|
}
|
|
391
415
|
|
|
392
416
|
/**
|
|
417
|
+
* :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
|
|
418
|
+
* While this may come as a surprise, the app providing the body ensures that only
|
|
419
|
+
* desired and correctly formatted data is sent with the request.
|
|
420
|
+
* :::
|
|
421
|
+
*
|
|
393
422
|
* Builds request options to create new record for resources,
|
|
394
423
|
* configured for the url, method and header expectations of most JSON:API APIs.
|
|
395
424
|
*
|
|
396
425
|
* **Basic Usage**
|
|
397
426
|
*
|
|
398
427
|
* ```ts
|
|
428
|
+
* import { cacheKeyFor } from '@warp-drive-mirror/core';
|
|
399
429
|
* import { createRecord } from '@warp-drive-mirror/utilities/json-api';
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
* const
|
|
430
|
+
* import type { Person } from '#/data/types';
|
|
431
|
+
*
|
|
432
|
+
* const person = store.createRecord<Person>('person', { name: 'Ted' });
|
|
433
|
+
* const init = createRecord(person);
|
|
434
|
+
* init.body = JSON.stringify(
|
|
435
|
+
* {
|
|
436
|
+
* // it's likely you will want to transform this data
|
|
437
|
+
* // somewhat
|
|
438
|
+
* data: store.cache.peek(cacheKeyFor(person))
|
|
439
|
+
* }
|
|
440
|
+
* );
|
|
441
|
+
* const data = await store.request(init);
|
|
403
442
|
* ```
|
|
404
443
|
*
|
|
405
444
|
* **Supplying Options to Modify the Request Behavior**
|
|
@@ -458,19 +497,34 @@ function createRecord(record, options = {}) {
|
|
|
458
497
|
}
|
|
459
498
|
|
|
460
499
|
/**
|
|
500
|
+
* :::warning ⚠️ **These Mutation Builders DO NOT Set The Necessary Request Body**
|
|
501
|
+
* While this may come as a surprise, the app providing the body ensures that only
|
|
502
|
+
* desired and correctly formatted data is sent with the request.
|
|
503
|
+
* :::
|
|
504
|
+
*
|
|
461
505
|
* Builds request options to update existing record for resources,
|
|
462
506
|
* configured for the url, method and header expectations of most JSON:API APIs.
|
|
463
507
|
*
|
|
464
|
-
* **
|
|
508
|
+
* **Example Usage**
|
|
465
509
|
*
|
|
466
510
|
* ```ts
|
|
511
|
+
* import { cacheKeyFor } from '@warp-drive-mirror/core';
|
|
467
512
|
* import { updateRecord } from '@warp-drive-mirror/utilities/json-api';
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
513
|
+
* import type { EditablePerson } from '#/data/types';
|
|
514
|
+
*
|
|
515
|
+
* const mutable = await checkout<EditablePerson>(person);
|
|
516
|
+
* mutable.name = 'Chris';
|
|
517
|
+
* const init = updateRecord(mutable);
|
|
518
|
+
*
|
|
519
|
+
* init.body = JSON.stringify(
|
|
520
|
+
* // it's likely you will want to transform this data
|
|
521
|
+
* // somewhat, or serialize only specific properties instead
|
|
522
|
+
* serializePatch(store.cache, cacheKeyFor(mutable))
|
|
523
|
+
* );
|
|
524
|
+
* const data = await store.request(init);
|
|
472
525
|
* ```
|
|
473
526
|
*
|
|
527
|
+
*
|
|
474
528
|
* **Supplying Options to Modify the Request Behavior**
|
|
475
529
|
*
|
|
476
530
|
* The following options are supported:
|
|
@@ -534,6 +588,13 @@ function updateRecord(record, options = {}) {
|
|
|
534
588
|
}
|
|
535
589
|
|
|
536
590
|
/**
|
|
591
|
+
* :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
|
|
592
|
+
*
|
|
593
|
+
* While this may come as a surprise, they are intended to serialize cache state for more
|
|
594
|
+
* generalized usage. {json:api} has a large variance in acceptable shapes, and only your
|
|
595
|
+
* app can ensure that the body is correctly formatted and contains all necessary data.
|
|
596
|
+
* :::
|
|
597
|
+
*
|
|
537
598
|
* Serializes the current state of a resource or array of resources for use with POST or PUT requests.
|
|
538
599
|
*
|
|
539
600
|
* @public
|
|
@@ -606,7 +667,14 @@ function _serializeResource(cache, identifier) {
|
|
|
606
667
|
}
|
|
607
668
|
|
|
608
669
|
/**
|
|
609
|
-
*
|
|
670
|
+
* :::warning ⚠️ **This util often won't produce the necessary body for a {json:api} request**
|
|
671
|
+
*
|
|
672
|
+
* While this may come as a surprise, they are intended to serialize cache state for more
|
|
673
|
+
* generalized usage. {json:api} has a large variance in acceptable shapes, and only your
|
|
674
|
+
* app can ensure that the body is correctly formatted and contains all necessary data.
|
|
675
|
+
* :::
|
|
676
|
+
*
|
|
677
|
+
* Serializes changes to a resource. Useful for use with building bodies for PATCH requests.
|
|
610
678
|
*
|
|
611
679
|
* Only attributes which are changed are serialized.
|
|
612
680
|
* Only relationships which are changed are serialized.
|
package/dist/rest.js
CHANGED
|
@@ -56,8 +56,6 @@ import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
|
56
56
|
* ```
|
|
57
57
|
*
|
|
58
58
|
* @public
|
|
59
|
-
* @param identifier
|
|
60
|
-
* @param options
|
|
61
59
|
*/
|
|
62
60
|
|
|
63
61
|
function findRecord(arg1, arg2, arg3) {
|
|
@@ -88,6 +86,8 @@ function findRecord(arg1, arg2, arg3) {
|
|
|
88
86
|
};
|
|
89
87
|
}
|
|
90
88
|
|
|
89
|
+
/** @deprecated use {@link ReactiveDataDocument} instead */
|
|
90
|
+
|
|
91
91
|
/**
|
|
92
92
|
* Builds request options to query for resources, usually by a primary
|
|
93
93
|
* type, configured for the url and header expectations of most REST APIs.
|
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 };
|