@cedarjs/utils 3.0.0-canary.13524
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/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/cedarPluralize.d.ts +24 -0
- package/dist/cedarPluralize.d.ts.map +1 -0
- package/dist/cedarPluralize.js +65 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Cedar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Utils
|
|
2
|
+
|
|
3
|
+
Shared utilities for CedarJS packages and Cedar apps.
|
|
4
|
+
|
|
5
|
+
## Utilities
|
|
6
|
+
|
|
7
|
+
### `cedarPluralize`
|
|
8
|
+
|
|
9
|
+
A thin wrapper around the [`pluralize`](https://github.com/plurals/pluralize)
|
|
10
|
+
library that adds support for PascalCased multi-word identifiers (e.g.
|
|
11
|
+
`DataModel` -> `DataModels`) and a custom singular/plural mapping registry.
|
|
12
|
+
|
|
13
|
+
#### Functions
|
|
14
|
+
|
|
15
|
+
| Function | Description |
|
|
16
|
+
| ------------------------------------- | ---------------------------------------------- |
|
|
17
|
+
| `pluralize(word)` | Returns the plural form of the given word |
|
|
18
|
+
| `singularize(word)` | Returns the singular form of the given word |
|
|
19
|
+
| `isPlural(word)` | Returns `true` if the (last) word is plural |
|
|
20
|
+
| `isSingular(word)` | Returns `true` if the (last) word is singular |
|
|
21
|
+
| `addSingularPlural(singular, plural)` | Registers a custom singular <-> plural mapping |
|
|
22
|
+
|
|
23
|
+
#### Usage
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import {
|
|
27
|
+
pluralize,
|
|
28
|
+
singularize,
|
|
29
|
+
isPlural,
|
|
30
|
+
isSingular,
|
|
31
|
+
addSingularPlural,
|
|
32
|
+
} from '@cedarjs/utils/cedarPluralize'
|
|
33
|
+
|
|
34
|
+
pluralize('book') // 'books'
|
|
35
|
+
pluralize('DataModel') // 'DataModels'
|
|
36
|
+
singularize('teeth') // 'tooth'
|
|
37
|
+
isPlural('books') // true
|
|
38
|
+
isSingular('tooth') // true
|
|
39
|
+
|
|
40
|
+
addSingularPlural('Pokemon', 'Pokemonii')
|
|
41
|
+
pluralize('Pokemon') // 'Pokemonii'
|
|
42
|
+
```
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the plural form of the given word.
|
|
3
|
+
*/
|
|
4
|
+
export declare function pluralize(word: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Returns the singular form of the given word.
|
|
7
|
+
*/
|
|
8
|
+
export declare function singularize(word: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Returns true if the (last) word is plural.
|
|
11
|
+
*/
|
|
12
|
+
export declare function isPlural(word: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Returns true if the (last) word is singular.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isSingular(word: string): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Adds a new mapping from singular to plural.
|
|
19
|
+
*
|
|
20
|
+
* This function also maintains reverse mappings and ensures any previous
|
|
21
|
+
* conflicting mapping is removed (tests rely on this behavior).
|
|
22
|
+
*/
|
|
23
|
+
export declare function addSingularPlural(singular: string, plural: string): void;
|
|
24
|
+
//# sourceMappingURL=cedarPluralize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cedarPluralize.d.ts","sourceRoot":"","sources":["../src/cedarPluralize.ts"],"names":[],"mappings":"AAiCA;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAc9C;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAahD;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE9C;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEhD;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAcxE"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import plurals from "pluralize";
|
|
2
|
+
const mappings = {
|
|
3
|
+
toSingular: {},
|
|
4
|
+
toPlural: {}
|
|
5
|
+
};
|
|
6
|
+
function lastWord(str) {
|
|
7
|
+
const capitals = str.match(/[A-Z]/g) ?? [];
|
|
8
|
+
if (capitals.length === 0) {
|
|
9
|
+
return str;
|
|
10
|
+
}
|
|
11
|
+
const lastCapital = capitals[capitals.length - 1];
|
|
12
|
+
const lastIndex = str.lastIndexOf(lastCapital);
|
|
13
|
+
if (lastIndex >= 0) {
|
|
14
|
+
return str.slice(lastIndex);
|
|
15
|
+
}
|
|
16
|
+
return str;
|
|
17
|
+
}
|
|
18
|
+
function pluralize(word) {
|
|
19
|
+
if (mappings.toPlural[word]) {
|
|
20
|
+
return mappings.toPlural[word];
|
|
21
|
+
}
|
|
22
|
+
const singular = lastWord(word);
|
|
23
|
+
const base = word.slice(0, word.length - singular.length);
|
|
24
|
+
if (mappings.toPlural[singular]) {
|
|
25
|
+
return base + mappings.toPlural[singular];
|
|
26
|
+
}
|
|
27
|
+
return base + plurals.plural(singular);
|
|
28
|
+
}
|
|
29
|
+
function singularize(word) {
|
|
30
|
+
if (mappings.toSingular[word]) {
|
|
31
|
+
return mappings.toSingular[word];
|
|
32
|
+
}
|
|
33
|
+
const plural = lastWord(word);
|
|
34
|
+
const base = word.slice(0, word.length - plural.length);
|
|
35
|
+
if (mappings.toSingular[plural]) {
|
|
36
|
+
return base + mappings.toSingular[plural];
|
|
37
|
+
}
|
|
38
|
+
return base + plurals.singular(plural);
|
|
39
|
+
}
|
|
40
|
+
function isPlural(word) {
|
|
41
|
+
return plurals.isPlural(lastWord(word));
|
|
42
|
+
}
|
|
43
|
+
function isSingular(word) {
|
|
44
|
+
return plurals.isSingular(lastWord(word));
|
|
45
|
+
}
|
|
46
|
+
function addSingularPlural(singular, plural) {
|
|
47
|
+
const existingPlural = Object.keys(mappings.toSingular).find((key) => {
|
|
48
|
+
return mappings.toSingular[key] === singular;
|
|
49
|
+
});
|
|
50
|
+
if (existingPlural !== void 0) {
|
|
51
|
+
delete mappings.toSingular[existingPlural];
|
|
52
|
+
delete mappings.toPlural[existingPlural];
|
|
53
|
+
}
|
|
54
|
+
mappings.toPlural[singular] = plural;
|
|
55
|
+
mappings.toPlural[plural] = plural;
|
|
56
|
+
mappings.toSingular[plural] = singular;
|
|
57
|
+
mappings.toSingular[singular] = singular;
|
|
58
|
+
}
|
|
59
|
+
export {
|
|
60
|
+
addSingularPlural,
|
|
61
|
+
isPlural,
|
|
62
|
+
isSingular,
|
|
63
|
+
pluralize,
|
|
64
|
+
singularize
|
|
65
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cedarjs/utils",
|
|
3
|
+
"version": "3.0.0-canary.13524+64f541c46",
|
|
4
|
+
"description": "Shared utilities for CedarJS packages and apps",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/cedarjs/cedar.git",
|
|
8
|
+
"directory": "packages/utils"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
"./cedarPluralize": {
|
|
14
|
+
"types": "./dist/cedarPluralize.d.ts",
|
|
15
|
+
"default": "./dist/cedarPluralize.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "node ./build.mts",
|
|
23
|
+
"build:pack": "yarn pack -o cedarjs-utils.tgz",
|
|
24
|
+
"build:types": "tsc --build --verbose ./tsconfig.build.json",
|
|
25
|
+
"build:watch": "nodemon --watch src --ext \"js,ts,tsx\" --ignore dist --exec \"yarn build\"",
|
|
26
|
+
"check:package": "yarn:publint",
|
|
27
|
+
"prepublishOnly": "NODE_ENV=production yarn build",
|
|
28
|
+
"test": "vitest run",
|
|
29
|
+
"test:watch": "vitest watch"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"pluralize": "8.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@cedarjs/framework-tools": "3.0.0-canary.13524",
|
|
36
|
+
"@types/pluralize": "0.0.33",
|
|
37
|
+
"nodemon": "3.1.14",
|
|
38
|
+
"publint": "0.3.18",
|
|
39
|
+
"typescript": "5.9.3",
|
|
40
|
+
"vitest": "3.2.4"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"gitHead": "64f541c46831ac89850fc7c4f84bc7820ba3c6d3"
|
|
46
|
+
}
|