Skip to content

Commit

Permalink
infra(unicorn): prefer-code-point (#2509)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Nov 23, 2023
1 parent aff0f80 commit 9b00fe9
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/modules/finance/iban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
),
};

Expand Down
20 changes: 11 additions & 9 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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('');

Expand Down
8 changes: 5 additions & 3 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
8 changes: 8 additions & 0 deletions test/modules/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down

0 comments on commit 9b00fe9

Please sign in to comment.