utilitish 0.0.11 → 0.0.14
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.txt +25 -25
- package/README.md +68 -68
- package/dist/object/object-prototype.spec.js +2 -0
- package/dist/string/string-prototype.d.ts +0 -20
- package/dist/string/string-prototype.js +0 -9
- package/dist/string/string-prototype.spec.js +0 -36
- package/dist/test.html +13 -0
- package/dist/utils/slugify.config.d.ts +2 -1
- package/dist/utils/slugify.config.js +6 -5
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +15 -0
- package/package.json +4 -2
package/LICENSE.txt
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
GNU GENERAL PUBLIC LICENSE
|
|
2
|
-
Version 3, 29 June 2007
|
|
3
|
-
|
|
4
|
-
Copyright (C) 2025 Donovan Ferreira
|
|
5
|
-
|
|
6
|
-
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
|
7
|
-
|
|
8
|
-
Preamble
|
|
9
|
-
|
|
10
|
-
The GNU General Public License is a free, copyleft license for
|
|
11
|
-
software and other kinds of works.
|
|
12
|
-
|
|
13
|
-
The licenses for most software and other practical works are designed
|
|
14
|
-
to take away your freedom to share and change the works. By contrast,
|
|
15
|
-
the GNU General Public License is intended to guarantee your freedom to
|
|
16
|
-
share and change all versions of a program--to make sure it remains free
|
|
17
|
-
software for all its users. We, the Free Software Foundation, use the
|
|
18
|
-
GNU General Public License for most of our software; it applies also to
|
|
19
|
-
any other work released this way by its authors. You can apply it to
|
|
20
|
-
your programs, too.
|
|
21
|
-
|
|
22
|
-
[... contenu tronqué pour l’aperçu ...]
|
|
23
|
-
|
|
24
|
-
You should have received a copy of the GNU General Public License
|
|
25
|
-
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
1
|
+
GNU GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2025 Donovan Ferreira
|
|
5
|
+
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
Preamble
|
|
9
|
+
|
|
10
|
+
The GNU General Public License is a free, copyleft license for
|
|
11
|
+
software and other kinds of works.
|
|
12
|
+
|
|
13
|
+
The licenses for most software and other practical works are designed
|
|
14
|
+
to take away your freedom to share and change the works. By contrast,
|
|
15
|
+
the GNU General Public License is intended to guarantee your freedom to
|
|
16
|
+
share and change all versions of a program--to make sure it remains free
|
|
17
|
+
software for all its users. We, the Free Software Foundation, use the
|
|
18
|
+
GNU General Public License for most of our software; it applies also to
|
|
19
|
+
any other work released this way by its authors. You can apply it to
|
|
20
|
+
your programs, too.
|
|
21
|
+
|
|
22
|
+
[... contenu tronqué pour l’aperçu ...]
|
|
23
|
+
|
|
24
|
+
You should have received a copy of the GNU General Public License
|
|
25
|
+
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
package/README.md
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
## Licence
|
|
2
|
-
|
|
3
|
-
Ce projet est distribué sous licence [GPL v3](https://www.gnu.org/licenses/gpl-3.0.html) © 2025 Donovan Ferreira.
|
|
4
|
-
|
|
5
|
-
Ceci est une petite librairie qui ajoute plusieurs méthodes au prototype JavaScript.
|
|
6
|
-
|
|
7
|
-
## Configuration Slugify
|
|
8
|
-
|
|
9
|
-
La méthode `slugify()` peut être personnalisée pour répondre à vos besoins spécifiques. Consultez la JSDoc dans votre IDE pour voir tous les exemples disponibles.
|
|
10
|
-
|
|
11
|
-
### Configuration globale
|
|
12
|
-
|
|
13
|
-
Vous pouvez définir une configuration globale qui s'appliquera à tous les appels `slugify()` :
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { setSlugifyConfig } from 'utilitish';
|
|
17
|
-
|
|
18
|
-
// Configuration pour remplacer les symboles de genre
|
|
19
|
-
setSlugifyConfig(
|
|
20
|
-
SlugifyConfig.builder()
|
|
21
|
-
.withSeparator('_')
|
|
22
|
-
.withCustomReplacements({
|
|
23
|
-
'♀': 'feminin',
|
|
24
|
-
'♂': 'masculin',
|
|
25
|
-
'@': 'at',
|
|
26
|
-
})
|
|
27
|
-
.build(),
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
'Test ♀'.slugify(); // "test_feminin"
|
|
31
|
-
'User♂@domain.com'.slugify(); // "usermasculinatdomain_com"
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
### Configuration par appel
|
|
35
|
-
|
|
36
|
-
Vous pouvez aussi passer une configuration spécifique à chaque appel :
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
// Utilise la config globale
|
|
40
|
-
'Hello World'.slugify(); // "hello-world"
|
|
41
|
-
|
|
42
|
-
// Override la config pour cet appel
|
|
43
|
-
'Hello World'.slugify(SlugifyConfig.builder().withSeparator('_').build()); // "hello_world"
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### Options de configuration
|
|
47
|
-
|
|
48
|
-
- **`customReplacements`**: Remplacer des caractères spécifiques (ex: "♀" → "feminin")
|
|
49
|
-
- **`separator`**: Caractère de séparation (défaut: "-")
|
|
50
|
-
- **`lowercase`**: Convertir en minuscules (défaut: true)
|
|
51
|
-
- **`removeAccents`**: Supprimer les accents (défaut: true)
|
|
52
|
-
- **`allowedChars`**: Regex des caractères autorisés (défaut: /[a-zA-Z0-9]/)
|
|
53
|
-
- **`maxLength`**: Longueur maximale du slug
|
|
54
|
-
- **`transformers`**: Fonctions de transformation personnalisées
|
|
55
|
-
|
|
56
|
-
### Gestion de la configuration
|
|
57
|
-
|
|
58
|
-
```typescript
|
|
59
|
-
import { getSlugifyConfig, resetSlugifyConfig } from 'utilitish';
|
|
60
|
-
|
|
61
|
-
// Obtenir la config actuelle
|
|
62
|
-
const currentConfig = getSlugifyConfig();
|
|
63
|
-
|
|
64
|
-
// Réinitialiser aux valeurs par défaut
|
|
65
|
-
resetSlugifyConfig();
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
Pour plus d'exemples et cas d'usage avancés, consultez les JSDoc intégrées dans votre IDE.
|
|
1
|
+
## Licence
|
|
2
|
+
|
|
3
|
+
Ce projet est distribué sous licence [GPL v3](https://www.gnu.org/licenses/gpl-3.0.html) © 2025 Donovan Ferreira.
|
|
4
|
+
|
|
5
|
+
Ceci est une petite librairie qui ajoute plusieurs méthodes au prototype JavaScript.
|
|
6
|
+
|
|
7
|
+
## Configuration Slugify
|
|
8
|
+
|
|
9
|
+
La méthode `slugify()` peut être personnalisée pour répondre à vos besoins spécifiques. Consultez la JSDoc dans votre IDE pour voir tous les exemples disponibles.
|
|
10
|
+
|
|
11
|
+
### Configuration globale
|
|
12
|
+
|
|
13
|
+
Vous pouvez définir une configuration globale qui s'appliquera à tous les appels `slugify()` :
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { setSlugifyConfig } from 'utilitish';
|
|
17
|
+
|
|
18
|
+
// Configuration pour remplacer les symboles de genre
|
|
19
|
+
setSlugifyConfig(
|
|
20
|
+
SlugifyConfig.builder()
|
|
21
|
+
.withSeparator('_')
|
|
22
|
+
.withCustomReplacements({
|
|
23
|
+
'♀': 'feminin',
|
|
24
|
+
'♂': 'masculin',
|
|
25
|
+
'@': 'at',
|
|
26
|
+
})
|
|
27
|
+
.build(),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
'Test ♀'.slugify(); // "test_feminin"
|
|
31
|
+
'User♂@domain.com'.slugify(); // "usermasculinatdomain_com"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Configuration par appel
|
|
35
|
+
|
|
36
|
+
Vous pouvez aussi passer une configuration spécifique à chaque appel :
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
// Utilise la config globale
|
|
40
|
+
'Hello World'.slugify(); // "hello-world"
|
|
41
|
+
|
|
42
|
+
// Override la config pour cet appel
|
|
43
|
+
'Hello World'.slugify(SlugifyConfig.builder().withSeparator('_').build()); // "hello_world"
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Options de configuration
|
|
47
|
+
|
|
48
|
+
- **`customReplacements`**: Remplacer des caractères spécifiques (ex: "♀" → "feminin")
|
|
49
|
+
- **`separator`**: Caractère de séparation (défaut: "-")
|
|
50
|
+
- **`lowercase`**: Convertir en minuscules (défaut: true)
|
|
51
|
+
- **`removeAccents`**: Supprimer les accents (défaut: true)
|
|
52
|
+
- **`allowedChars`**: Regex des caractères autorisés (défaut: /[a-zA-Z0-9]/)
|
|
53
|
+
- **`maxLength`**: Longueur maximale du slug
|
|
54
|
+
- **`transformers`**: Fonctions de transformation personnalisées
|
|
55
|
+
|
|
56
|
+
### Gestion de la configuration
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { getSlugifyConfig, resetSlugifyConfig } from 'utilitish';
|
|
60
|
+
|
|
61
|
+
// Obtenir la config actuelle
|
|
62
|
+
const currentConfig = getSlugifyConfig();
|
|
63
|
+
|
|
64
|
+
// Réinitialiser aux valeurs par défaut
|
|
65
|
+
resetSlugifyConfig();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Pour plus d'exemples et cas d'usage avancés, consultez les JSDoc intégrées dans votre IDE.
|
|
@@ -24,11 +24,13 @@ describe('Object.prototype', () => {
|
|
|
24
24
|
const source = { b: { d: 3 }, e: 4 };
|
|
25
25
|
const merged = obj.deepMerge(source);
|
|
26
26
|
expect(merged).toEqual({ a: 1, b: { c: 2, d: 3 }, e: 4 });
|
|
27
|
+
console.log(merged);
|
|
27
28
|
});
|
|
28
29
|
it('should overwrite primitive values', () => {
|
|
29
30
|
const obj = { a: 1, b: 2 };
|
|
30
31
|
const merged = obj.deepMerge({ b: 3 });
|
|
31
32
|
expect(merged).toEqual({ a: 1, b: 3 });
|
|
33
|
+
console.log(merged);
|
|
32
34
|
});
|
|
33
35
|
describe('error handling', () => {
|
|
34
36
|
it('should throw if source is not an object', () => {
|
|
@@ -194,26 +194,6 @@ declare global {
|
|
|
194
194
|
*/
|
|
195
195
|
slugify(): string;
|
|
196
196
|
slugify(config: SlugifyConfig): string;
|
|
197
|
-
/**
|
|
198
|
-
* Compares two strings by slugifying both and checking if they are equal.
|
|
199
|
-
* Useful for case-insensitive and accent-insensitive string comparison.
|
|
200
|
-
*
|
|
201
|
-
* @this {string} The first string to compare
|
|
202
|
-
* @param {string} other - The second string to compare
|
|
203
|
-
* @returns {boolean} True if both slugified strings are equal, false otherwise
|
|
204
|
-
* @throws {TypeError} If the parameter is not a string
|
|
205
|
-
*
|
|
206
|
-
* @example
|
|
207
|
-
* 'Hello World'.compareSlugify('hello-world'); // true
|
|
208
|
-
* 'Héllo Wørld'.compareSlugify('hello-world'); // true
|
|
209
|
-
* 'Hello World'.compareSlugify('goodbye-world'); // false
|
|
210
|
-
*
|
|
211
|
-
* @remarks
|
|
212
|
-
* - Both strings are slugified using the default or global configuration
|
|
213
|
-
* - Useful for comparing user-provided strings with stored slugs
|
|
214
|
-
* - Guard ensures the parameter is a valid string type
|
|
215
|
-
*/
|
|
216
|
-
compareSlugify(other: string): boolean;
|
|
217
197
|
/**
|
|
218
198
|
* Replaces a substring between `start` and `end` indices with a given string.
|
|
219
199
|
* Provides precise control over which portion of the string to replace.
|
|
@@ -73,15 +73,6 @@ const slugify_utils_1 = require("../utils/slugify.utils");
|
|
|
73
73
|
return '';
|
|
74
74
|
return this.charAt(0).toUpperCase() + this.slice(1);
|
|
75
75
|
});
|
|
76
|
-
/**
|
|
77
|
-
* @see String.prototype.compareSlugify
|
|
78
|
-
*/
|
|
79
|
-
(0, core_utils_1.defineIfNotExists)(String.prototype, 'compareSlugify', function (other) {
|
|
80
|
-
if (typeof other !== 'string') {
|
|
81
|
-
throw new TypeError('Parameter must be a string');
|
|
82
|
-
}
|
|
83
|
-
return this.slugify() === other.slugify();
|
|
84
|
-
});
|
|
85
76
|
/**
|
|
86
77
|
* @see String.prototype.replaceRange
|
|
87
78
|
*/
|
|
@@ -161,42 +161,6 @@ describe('String.prototype', () => {
|
|
|
161
161
|
});
|
|
162
162
|
});
|
|
163
163
|
});
|
|
164
|
-
describe('compareSlugify()', () => {
|
|
165
|
-
beforeEach(() => {
|
|
166
|
-
(0, slugify_config_1.resetSlugifyConfig)(); // Reset to defaults before each test
|
|
167
|
-
});
|
|
168
|
-
it('should return true when slugified strings are equal', () => {
|
|
169
|
-
expect('Hello World'.compareSlugify('hello-world')).toBe(true);
|
|
170
|
-
expect('hello world'.compareSlugify('hello-world')).toBe(true);
|
|
171
|
-
expect('HELLO WORLD'.compareSlugify('hello-world')).toBe(true);
|
|
172
|
-
});
|
|
173
|
-
it('should handle accents and special characters', () => {
|
|
174
|
-
expect('Café'.compareSlugify('cafe')).toBe(true);
|
|
175
|
-
expect('Éléphant'.compareSlugify('elephant')).toBe(true);
|
|
176
|
-
expect('naïve café'.compareSlugify('naive-cafe')).toBe(true);
|
|
177
|
-
expect('résumé'.compareSlugify('resume')).toBe(true);
|
|
178
|
-
});
|
|
179
|
-
it('should return false when slugified strings are different', () => {
|
|
180
|
-
expect('Hello World'.compareSlugify('goodbye-world')).toBe(false);
|
|
181
|
-
expect('test string'.compareSlugify('different-string')).toBe(false);
|
|
182
|
-
});
|
|
183
|
-
it('should handle extra spaces and special characters', () => {
|
|
184
|
-
expect(' Hello --- World '.compareSlugify('hello-world')).toBe(true);
|
|
185
|
-
expect('Hello!!!World???'.compareSlugify('hello-world')).toBe(true);
|
|
186
|
-
});
|
|
187
|
-
it('should throw TypeError if parameter is not a string', () => {
|
|
188
|
-
expect(() => 'hello'.compareSlugify(123)).toThrowError(TypeError);
|
|
189
|
-
expect(() => 'hello'.compareSlugify(null)).toThrowError(TypeError);
|
|
190
|
-
expect(() => 'hello'.compareSlugify(undefined)).toThrowError(TypeError);
|
|
191
|
-
expect(() => 'hello'.compareSlugify({})).toThrowError(TypeError);
|
|
192
|
-
expect(() => 'hello'.compareSlugify([])).toThrowError(TypeError);
|
|
193
|
-
});
|
|
194
|
-
it('should work with empty strings', () => {
|
|
195
|
-
expect(''.compareSlugify('')).toBe(true);
|
|
196
|
-
expect(' '.compareSlugify('')).toBe(true);
|
|
197
|
-
expect('hello'.compareSlugify('')).toBe(false);
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
164
|
describe('replaceRange()', () => {
|
|
201
165
|
it('should replace a single character at the given index', () => {
|
|
202
166
|
expect('hello'.replaceRange(1, 2, 'a')).toBe('hallo');
|
package/dist/test.html
ADDED
|
@@ -10,6 +10,7 @@ export declare class SlugifyConfig {
|
|
|
10
10
|
private readonly _allowedChars?;
|
|
11
11
|
private readonly _maxLength?;
|
|
12
12
|
private readonly _transformers;
|
|
13
|
+
private readonly _isValidated;
|
|
13
14
|
private constructor();
|
|
14
15
|
static builder(): SlugifyConfigBuilder;
|
|
15
16
|
static default(): SlugifyConfig;
|
|
@@ -83,7 +84,7 @@ export declare function getDefaultSlugifyConfig(): SlugifyConfig;
|
|
|
83
84
|
* );
|
|
84
85
|
*/
|
|
85
86
|
export declare function assertCharClass(regex: RegExp): void;
|
|
86
|
-
export declare function
|
|
87
|
+
export declare function validateSlugifyConfig(builderOrConfig: SlugifyConfigBuilder | SlugifyConfig): void;
|
|
87
88
|
export declare function setSlugifyConfig(config: SlugifyConfig): void;
|
|
88
89
|
/**
|
|
89
90
|
* Gets the current global slugify configuration.
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SlugifyConfigBuilder = exports.SlugifyConfig = void 0;
|
|
4
4
|
exports.getDefaultSlugifyConfig = getDefaultSlugifyConfig;
|
|
5
5
|
exports.assertCharClass = assertCharClass;
|
|
6
|
-
exports.
|
|
6
|
+
exports.validateSlugifyConfig = validateSlugifyConfig;
|
|
7
7
|
exports.setSlugifyConfig = setSlugifyConfig;
|
|
8
8
|
exports.getSlugifyConfig = getSlugifyConfig;
|
|
9
9
|
exports.resetSlugifyConfig = resetSlugifyConfig;
|
|
@@ -12,7 +12,7 @@ exports.resetSlugifyConfig = resetSlugifyConfig;
|
|
|
12
12
|
* Provides comprehensive customization options for URL slug generation.
|
|
13
13
|
*/
|
|
14
14
|
class SlugifyConfig {
|
|
15
|
-
constructor(builder) {
|
|
15
|
+
constructor(builder, isValidated = false) {
|
|
16
16
|
this._customReplacements = builder.customReplacements;
|
|
17
17
|
this._separator = builder.separator;
|
|
18
18
|
this._case = builder.case;
|
|
@@ -20,6 +20,7 @@ class SlugifyConfig {
|
|
|
20
20
|
this._allowedChars = builder.allowedChars;
|
|
21
21
|
this._maxLength = builder.maxLength;
|
|
22
22
|
this._transformers = builder.transformers;
|
|
23
|
+
this._isValidated = isValidated;
|
|
23
24
|
}
|
|
24
25
|
static builder() {
|
|
25
26
|
return new SlugifyConfigBuilder();
|
|
@@ -28,8 +29,8 @@ class SlugifyConfig {
|
|
|
28
29
|
return SlugifyConfig.builder().build();
|
|
29
30
|
}
|
|
30
31
|
static create(builder) {
|
|
31
|
-
|
|
32
|
-
const config = new SlugifyConfig(builder);
|
|
32
|
+
validateSlugifyConfig(builder);
|
|
33
|
+
const config = new SlugifyConfig(builder, true);
|
|
33
34
|
return Object.freeze(config);
|
|
34
35
|
}
|
|
35
36
|
get customReplacements() {
|
|
@@ -216,7 +217,7 @@ function assertCharClass(regex) {
|
|
|
216
217
|
throw new Error(`Invalid allowedChars: must be a character class like /[a-z]/, received ${regex}`);
|
|
217
218
|
}
|
|
218
219
|
}
|
|
219
|
-
function
|
|
220
|
+
function validateSlugifyConfig(builderOrConfig) {
|
|
220
221
|
const allowedChars = builderOrConfig.allowedChars;
|
|
221
222
|
const separator = builderOrConfig.separator;
|
|
222
223
|
const caseValue = builderOrConfig.case;
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const defineIfNotExists: <T extends object>(prototype: T, name: string, fn: Function) => void;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineIfNotExists = void 0;
|
|
4
|
+
const defineIfNotExists = (prototype, name, fn) => {
|
|
5
|
+
const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
|
|
6
|
+
if (!descriptor || descriptor.writable || descriptor.configurable) {
|
|
7
|
+
Object.defineProperty(prototype, name, {
|
|
8
|
+
value: fn,
|
|
9
|
+
enumerable: false,
|
|
10
|
+
configurable: false,
|
|
11
|
+
writable: false,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
exports.defineIfNotExists = defineIfNotExists;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "utilitish",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/FDonovan12/utilitish.git"
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
"scripts": {
|
|
19
19
|
"build": "tsc",
|
|
20
20
|
"test": "jest",
|
|
21
|
-
"docs": "typedoc"
|
|
21
|
+
"docs": "typedoc",
|
|
22
|
+
"release": "npm version patch && git push --follow-tags && npm publish"
|
|
22
23
|
},
|
|
23
24
|
"keywords": [
|
|
24
25
|
"typescript",
|
|
@@ -31,6 +32,7 @@
|
|
|
31
32
|
"@types/jest": "^29.5.14",
|
|
32
33
|
"jest": "^29.7.0",
|
|
33
34
|
"ts-jest": "^29.3.4",
|
|
35
|
+
"ts-node": "^10.9.2",
|
|
34
36
|
"typedoc": "^0.28.10",
|
|
35
37
|
"typescript": "^5.8.3"
|
|
36
38
|
}
|