aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.eslintrc.js1
-rw-r--r--src/modules/finance/iban.ts2
-rw-r--r--src/modules/helpers/index.ts20
-rw-r--r--src/modules/internet/index.ts8
-rw-r--r--src/modules/string/index.ts2
-rw-r--r--test/modules/internet.spec.ts8
6 files changed, 26 insertions, 15 deletions
diff --git a/.eslintrc.js b/.eslintrc.js
index a4484d52..8458f6d2 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -60,7 +60,6 @@ module.exports = defineConfig({
'unicorn/no-object-as-default-parameter': 'off',
'unicorn/no-useless-switch-case': 'off',
'unicorn/numeric-separators-style': 'off',
- 'unicorn/prefer-code-point': 'off',
'unicorn/prefer-export-from': 'off',
'unicorn/prefer-string-slice': 'off',
'unicorn/prevent-abbreviations': 'off',
diff --git a/src/modules/finance/iban.ts b/src/modules/finance/iban.ts
index 8b3f78c4..d639404c 100644
--- a/src/modules/finance/iban.ts
+++ b/src/modules/finance/iban.ts
@@ -1408,7 +1408,7 @@ const iban: Iban = {
pattern100: ['001', '002', '003', '004', '005', '006', '007', '008', '009'],
toDigitString: (str) =>
str.replace(/[A-Z]/gi, (match) =>
- String(match.toUpperCase().charCodeAt(0) - 55)
+ String((match.toUpperCase().codePointAt(0) ?? Number.NaN) - 55)
),
};
diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts
index 3f121a41..aaf0e231 100644
--- a/src/modules/helpers/index.ts
+++ b/src/modules/helpers/index.ts
@@ -458,7 +458,9 @@ export class SimpleHelpersModule extends SimpleModuleBase {
while (range != null) {
if (range[0].includes('-')) {
// handle ranges
- const rangeMinMax = range[0].split('-').map((x) => x.charCodeAt(0));
+ const rangeMinMax = range[0]
+ .split('-')
+ .map((x) => x.codePointAt(0) ?? Number.NaN);
min = rangeMinMax[0];
max = rangeMinMax[1];
// throw error if min larger than max
@@ -469,12 +471,12 @@ export class SimpleHelpersModule extends SimpleModuleBase {
for (let i = min; i <= max; i++) {
if (
isCaseInsensitive &&
- Number.isNaN(Number(String.fromCharCode(i)))
+ Number.isNaN(Number(String.fromCodePoint(i)))
) {
- const ch = String.fromCharCode(i);
+ const ch = String.fromCodePoint(i);
rangeCodes.push(
- ch.toUpperCase().charCodeAt(0),
- ch.toLowerCase().charCodeAt(0)
+ ch.toUpperCase().codePointAt(0) ?? Number.NaN,
+ ch.toLowerCase().codePointAt(0) ?? Number.NaN
);
} else {
rangeCodes.push(i);
@@ -484,11 +486,11 @@ export class SimpleHelpersModule extends SimpleModuleBase {
// handle non-ranges
if (isCaseInsensitive && Number.isNaN(Number(range[0]))) {
rangeCodes.push(
- range[0].toUpperCase().charCodeAt(0),
- range[0].toLowerCase().charCodeAt(0)
+ range[0].toUpperCase().codePointAt(0) ?? Number.NaN,
+ range[0].toLowerCase().codePointAt(0) ?? Number.NaN
);
} else {
- rangeCodes.push(range[0].charCodeAt(0));
+ rangeCodes.push(range[0].codePointAt(0) ?? Number.NaN);
}
}
@@ -540,7 +542,7 @@ export class SimpleHelpersModule extends SimpleModuleBase {
}
const generatedString = this.multiple(
- () => String.fromCharCode(this.arrayElement(rangeCodes)),
+ () => String.fromCodePoint(this.arrayElement(rangeCodes)),
{ count: repetitions }
).join('');
diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts
index 8367825f..395266fb 100644
--- a/src/modules/internet/index.ts
+++ b/src/modules/internet/index.ts
@@ -660,13 +660,15 @@ export class InternetModule extends ModuleBase {
return charMapping[char];
}
- if (char.charCodeAt(0) < 0x80) {
+ const charCode = char.codePointAt(0) ?? Number.NaN;
+
+ if (charCode < 0x80) {
// Keep ASCII characters
return char;
}
// Final fallback return the Unicode char code value for Chinese, Japanese, Korean etc, base-36 encoded
- return char.charCodeAt(0).toString(36);
+ return charCode.toString(36);
})
.join('');
result = result.toString().replace(/'/g, '');
@@ -1495,7 +1497,7 @@ export class InternetModule extends ModuleBase {
}
const n = this.faker.number.int(94) + 33;
- let char = String.fromCharCode(n);
+ let char = String.fromCodePoint(n);
if (memorable) {
char = char.toLowerCase();
}
diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts
index 4240a6f1..6428153d 100644
--- a/src/modules/string/index.ts
+++ b/src/modules/string/index.ts
@@ -674,7 +674,7 @@ export class StringModule extends SimpleModuleBase {
let returnString = '';
while (returnString.length < length) {
- returnString += String.fromCharCode(
+ returnString += String.fromCodePoint(
this.faker.number.int(charCodeOption)
);
}
diff --git a/test/modules/internet.spec.ts b/test/modules/internet.spec.ts
index be9aa025..e4c08b92 100644
--- a/test/modules/internet.spec.ts
+++ b/test/modules/internet.spec.ts
@@ -422,6 +422,14 @@ describe('internet', () => {
const username = faker.internet.userName('大羽', '陳');
expect(username).includes('hlzp8d');
});
+
+ it('should provide a fallback special unicode characters', () => {
+ const username = faker.internet.userName({
+ firstName: '🐼',
+ lastName: '❤️',
+ });
+ expect(username).includes('2qt8');
+ });
});
describe('displayName()', () => {