utilitish 0.0.10 → 0.0.11
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
CHANGED
|
@@ -16,16 +16,19 @@ Vous pouvez définir une configuration globale qui s'appliquera à tous les appe
|
|
|
16
16
|
import { setSlugifyConfig } from 'utilitish';
|
|
17
17
|
|
|
18
18
|
// Configuration pour remplacer les symboles de genre
|
|
19
|
-
setSlugifyConfig(
|
|
20
|
-
|
|
21
|
-
'
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
setSlugifyConfig(
|
|
20
|
+
SlugifyConfig.builder()
|
|
21
|
+
.withSeparator('_')
|
|
22
|
+
.withCustomReplacements({
|
|
23
|
+
'♀': 'feminin',
|
|
24
|
+
'♂': 'masculin',
|
|
25
|
+
'@': 'at',
|
|
26
|
+
})
|
|
27
|
+
.build(),
|
|
28
|
+
);
|
|
26
29
|
|
|
27
30
|
'Test ♀'.slugify(); // "test_feminin"
|
|
28
|
-
'User♂@domain.com'.slugify(); // "
|
|
31
|
+
'User♂@domain.com'.slugify(); // "usermasculinatdomain_com"
|
|
29
32
|
```
|
|
30
33
|
|
|
31
34
|
### Configuration par appel
|
|
@@ -37,7 +40,7 @@ Vous pouvez aussi passer une configuration spécifique à chaque appel :
|
|
|
37
40
|
'Hello World'.slugify(); // "hello-world"
|
|
38
41
|
|
|
39
42
|
// Override la config pour cet appel
|
|
40
|
-
'Hello World'.slugify(
|
|
43
|
+
'Hello World'.slugify(SlugifyConfig.builder().withSeparator('_').build()); // "hello_world"
|
|
41
44
|
```
|
|
42
45
|
|
|
43
46
|
### Options de configuration
|
|
@@ -24,13 +24,11 @@ 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);
|
|
28
27
|
});
|
|
29
28
|
it('should overwrite primitive values', () => {
|
|
30
29
|
const obj = { a: 1, b: 2 };
|
|
31
30
|
const merged = obj.deepMerge({ b: 3 });
|
|
32
31
|
expect(merged).toEqual({ a: 1, b: 3 });
|
|
33
|
-
console.log(merged);
|
|
34
32
|
});
|
|
35
33
|
describe('error handling', () => {
|
|
36
34
|
it('should throw if source is not an object', () => {
|
|
@@ -194,6 +194,26 @@ 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;
|
|
197
217
|
/**
|
|
198
218
|
* Replaces a substring between `start` and `end` indices with a given string.
|
|
199
219
|
* Provides precise control over which portion of the string to replace.
|
|
@@ -73,6 +73,15 @@ 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
|
+
});
|
|
76
85
|
/**
|
|
77
86
|
* @see String.prototype.replaceRange
|
|
78
87
|
*/
|
|
@@ -161,6 +161,42 @@ 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
|
+
});
|
|
164
200
|
describe('replaceRange()', () => {
|
|
165
201
|
it('should replace a single character at the given index', () => {
|
|
166
202
|
expect('hello'.replaceRange(1, 2, 'a')).toBe('hallo');
|