@poppinss/utils 6.3.0-0 → 6.4.0-0
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 +17 -4
- package/build/index.d.ts +2 -1
- package/build/index.js +2 -1
- package/build/src/exceptions/invalid_arguments_exception.d.ts +5 -0
- package/build/src/exceptions/invalid_arguments_exception.js +5 -0
- package/build/src/exceptions/runtime_exception.d.ts +0 -4
- package/build/src/exceptions/runtime_exception.js +0 -4
- package/build/src/string_builder.d.ts +23 -0
- package/build/src/string_builder.js +86 -0
- package/package.json +11 -10
package/README.md
CHANGED
|
@@ -556,6 +556,17 @@ string.bytes.format(1024 * 1024 * 1000) // 1000MB
|
|
|
556
556
|
string.bytes.format(1024 * 1024 * 1000, { thousandsSeparator: ',' }) // 1,000MB
|
|
557
557
|
```
|
|
558
558
|
|
|
559
|
+
### String builder
|
|
560
|
+
The string builder offers a fluent API for applying a set of transforms on a string value. You can create an instance of the string builder as follows.
|
|
561
|
+
|
|
562
|
+
```ts
|
|
563
|
+
import StringBuilder from '@poppinss/utils/string_builder'
|
|
564
|
+
const builder = new StringBuilder('hello world')
|
|
565
|
+
|
|
566
|
+
const value = builder.snakeCase().suffix('_controller').toString()
|
|
567
|
+
assert(value === 'hello_world_controller')
|
|
568
|
+
```
|
|
569
|
+
|
|
559
570
|
### JSON helpers
|
|
560
571
|
|
|
561
572
|
Following are the helpers we use to `stringify` and `parse` JSON.
|
|
@@ -772,6 +783,7 @@ throw new ResourceNotFound()
|
|
|
772
783
|
```
|
|
773
784
|
|
|
774
785
|
#### Anonymous error classes
|
|
786
|
+
|
|
775
787
|
You can also create an anonymous exception class using the `createError` method. The return value is a class
|
|
776
788
|
constructor that accepts an array of values to use for interpolation.
|
|
777
789
|
|
|
@@ -779,7 +791,10 @@ The interpolation of error message is performed using the `util.format` message.
|
|
|
779
791
|
|
|
780
792
|
```ts
|
|
781
793
|
import { createError } from '@poppinss/utils'
|
|
782
|
-
const E_RESOURCE_NOT_FOUND = createError(
|
|
794
|
+
const E_RESOURCE_NOT_FOUND = createError(
|
|
795
|
+
'Unable to find resource with id %d',
|
|
796
|
+
'E_RESOURCE_NOT_FOUND'
|
|
797
|
+
)
|
|
783
798
|
|
|
784
799
|
const id = 1
|
|
785
800
|
throw new E_RESOURCE_NOT_FOUND([id])
|
|
@@ -1039,9 +1054,7 @@ Instead of writing conditionals, you can consider using the Object builder fluen
|
|
|
1039
1054
|
```ts
|
|
1040
1055
|
const builder = new ObjectBuilder({ a: 1 })
|
|
1041
1056
|
|
|
1042
|
-
const plainObject = builder
|
|
1043
|
-
.add('b', b)
|
|
1044
|
-
.toObject()
|
|
1057
|
+
const plainObject = builder.add('b', b).toObject()
|
|
1045
1058
|
```
|
|
1046
1059
|
|
|
1047
1060
|
By default, only the `undefined` values are ignored. However, you can also ignore `null` values.
|
package/build/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ export { naturalSort } from './src/natural_sort.js';
|
|
|
12
12
|
export { ObjectBuilder } from './src/object_builder.js';
|
|
13
13
|
export { safeEqual } from './src/safe_equal.js';
|
|
14
14
|
export { slash } from './src/slash.js';
|
|
15
|
-
export { RuntimeException
|
|
15
|
+
export { RuntimeException } from './src/exceptions/runtime_exception.js';
|
|
16
|
+
export { InvalidArgumentsException } from './src/exceptions/invalid_arguments_exception.js';
|
|
16
17
|
export declare function getDirname(url: string | URL): string;
|
|
17
18
|
export declare function getFilename(url: string | URL): string;
|
package/build/index.js
CHANGED
|
@@ -13,7 +13,8 @@ export { naturalSort } from './src/natural_sort.js';
|
|
|
13
13
|
export { ObjectBuilder } from './src/object_builder.js';
|
|
14
14
|
export { safeEqual } from './src/safe_equal.js';
|
|
15
15
|
export { slash } from './src/slash.js';
|
|
16
|
-
export { RuntimeException
|
|
16
|
+
export { RuntimeException } from './src/exceptions/runtime_exception.js';
|
|
17
|
+
export { InvalidArgumentsException } from './src/exceptions/invalid_arguments_exception.js';
|
|
17
18
|
export function getDirname(url) {
|
|
18
19
|
return pathDirname(getFilename(url));
|
|
19
20
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default class StringBuilder {
|
|
2
|
+
#private;
|
|
3
|
+
constructor(value: string | StringBuilder);
|
|
4
|
+
dashCase(): this;
|
|
5
|
+
dotCase(): this;
|
|
6
|
+
snakeCase(): this;
|
|
7
|
+
pascalCase(): this;
|
|
8
|
+
camelCase(): this;
|
|
9
|
+
capitalCase(): this;
|
|
10
|
+
titleCase(): this;
|
|
11
|
+
sentenceCase(): this;
|
|
12
|
+
noCase(): this;
|
|
13
|
+
plural(): this;
|
|
14
|
+
singular(): this;
|
|
15
|
+
slugify(): this;
|
|
16
|
+
removeSuffix(suffix: string): this;
|
|
17
|
+
suffix(suffix: string): this;
|
|
18
|
+
removePrefix(prefix: string): this;
|
|
19
|
+
prefix(prefix: string): this;
|
|
20
|
+
removeExtension(): this;
|
|
21
|
+
ext(extension: string): this;
|
|
22
|
+
toString(): string;
|
|
23
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { extname } from 'node:path';
|
|
2
|
+
import string from './string/main.js';
|
|
3
|
+
export default class StringBuilder {
|
|
4
|
+
#value;
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this.#value = typeof value === 'string' ? value : value.toString();
|
|
7
|
+
}
|
|
8
|
+
dashCase() {
|
|
9
|
+
this.#value = string.dashCase(this.#value);
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
12
|
+
dotCase() {
|
|
13
|
+
this.#value = string.dotCase(this.#value);
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
snakeCase() {
|
|
17
|
+
this.#value = string.snakeCase(this.#value);
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
pascalCase() {
|
|
21
|
+
this.#value = string.pascalCase(this.#value);
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
camelCase() {
|
|
25
|
+
this.#value = string.camelCase(this.#value);
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
capitalCase() {
|
|
29
|
+
this.#value = string.capitalCase(this.#value);
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
titleCase() {
|
|
33
|
+
this.#value = string.titleCase(this.#value);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
sentenceCase() {
|
|
37
|
+
this.#value = string.sentenceCase(this.#value);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
noCase() {
|
|
41
|
+
this.#value = string.noCase(this.#value);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
plural() {
|
|
45
|
+
this.#value = string.pluralize(this.#value);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
singular() {
|
|
49
|
+
this.#value = string.singular(this.#value);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
slugify() {
|
|
53
|
+
this.#value = string.slug(this.#value);
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
removeSuffix(suffix) {
|
|
57
|
+
this.#value = this.#value.replace(new RegExp(`[-_]?${suffix}$`, 'i'), '');
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
suffix(suffix) {
|
|
61
|
+
this.removeSuffix(suffix);
|
|
62
|
+
this.#value = `${this.#value}${suffix}`;
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
removePrefix(prefix) {
|
|
66
|
+
this.#value = this.#value.replace(new RegExp(`^${prefix}[-_]?`, 'i'), '');
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
prefix(prefix) {
|
|
70
|
+
this.removePrefix(prefix);
|
|
71
|
+
this.#value = `${prefix}${this.#value}`;
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
removeExtension() {
|
|
75
|
+
this.#value = this.#value.replace(new RegExp(`${extname(this.#value)}$`), '');
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
ext(extension) {
|
|
79
|
+
this.removeExtension();
|
|
80
|
+
this.#value = `${this.#value}.${extension.replace(/^\./, '')}`;
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
toString() {
|
|
84
|
+
return this.#value;
|
|
85
|
+
}
|
|
86
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@poppinss/utils",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.4.0-0",
|
|
4
4
|
"description": "Handy utilities for repetitive work",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"node": "./build/lodash/main.cjs"
|
|
19
19
|
},
|
|
20
20
|
"./string": "./build/src/string/main.js",
|
|
21
|
+
"./string_builder": "./build/src/string_builder.js",
|
|
21
22
|
"./json": "./build/src/json/main.js",
|
|
22
23
|
"./types": "./build/src/types.js"
|
|
23
24
|
},
|
|
@@ -53,30 +54,30 @@
|
|
|
53
54
|
"author": "virk,poppinss",
|
|
54
55
|
"license": "MIT",
|
|
55
56
|
"devDependencies": {
|
|
56
|
-
"@commitlint/cli": "^17.
|
|
57
|
-
"@commitlint/config-conventional": "^17.
|
|
57
|
+
"@commitlint/cli": "^17.4.2",
|
|
58
|
+
"@commitlint/config-conventional": "^17.4.2",
|
|
58
59
|
"@japa/assert": "^1.3.6",
|
|
59
60
|
"@japa/expect-type": "^1.0.2",
|
|
60
61
|
"@japa/run-failed-tests": "^1.1.0",
|
|
61
62
|
"@japa/runner": "^2.2.2",
|
|
62
63
|
"@japa/spec-reporter": "^1.3.2",
|
|
63
|
-
"@swc/core": "^1.3.
|
|
64
|
-
"@types/fs-extra": "^
|
|
64
|
+
"@swc/core": "^1.3.27",
|
|
65
|
+
"@types/fs-extra": "^11.0.1",
|
|
65
66
|
"@types/node": "^18.11.18",
|
|
66
67
|
"c8": "^7.12.0",
|
|
67
68
|
"del-cli": "^5.0.0",
|
|
68
|
-
"eslint": "^8.
|
|
69
|
-
"eslint-config-prettier": "^8.
|
|
69
|
+
"eslint": "^8.32.0",
|
|
70
|
+
"eslint-config-prettier": "^8.6.0",
|
|
70
71
|
"eslint-plugin-adonis": "^3.0.3",
|
|
71
72
|
"eslint-plugin-prettier": "^4.2.1",
|
|
72
73
|
"fs-extra": "^11.1.0",
|
|
73
74
|
"github-label-sync": "^2.2.0",
|
|
74
|
-
"husky": "^8.0.
|
|
75
|
+
"husky": "^8.0.3",
|
|
75
76
|
"lodash": "^4.17.21",
|
|
76
77
|
"lodash-cli": "^4.17.5",
|
|
77
78
|
"move-file-cli": "^3.0.0",
|
|
78
79
|
"np": "^7.6.3",
|
|
79
|
-
"prettier": "^2.8.
|
|
80
|
+
"prettier": "^2.8.3",
|
|
80
81
|
"ts-node": "^10.9.1",
|
|
81
82
|
"typescript": "^4.9.4"
|
|
82
83
|
},
|
|
@@ -89,7 +90,7 @@
|
|
|
89
90
|
"flattie": "^1.1.0",
|
|
90
91
|
"pluralize": "^8.0.0",
|
|
91
92
|
"safe-stable-stringify": "^2.4.2",
|
|
92
|
-
"secure-json-parse": "^2.
|
|
93
|
+
"secure-json-parse": "^2.7.0",
|
|
93
94
|
"slash": "^5.0.0",
|
|
94
95
|
"slugify": "^1.6.5",
|
|
95
96
|
"truncatise": "^0.0.8"
|