Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra(unicorn): prefer-code-point #2509

Merged
merged 5 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
// 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

Check warning on line 490 in src/modules/helpers/index.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/helpers/index.ts#L489-L490

Added lines #L489 - L490 were not covered by tests
);
} else {
rangeCodes.push(range[0].charCodeAt(0));
rangeCodes.push(range[0].codePointAt(0) ?? Number.NaN);
}
}

Expand Down Expand Up @@ -540,7 +542,7 @@
}

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');
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('displayName()', () => {
Expand Down