Skip to content
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
18 changes: 18 additions & 0 deletions src/compat/_internal/createPadding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// eslint-disable-next-line no-misleading-character-class
const regexMultiByte = /[\u200d\ud800-\udfff\u0300-\u036f\ufe20-\ufe2f\u20d0-\u20ff\ufe0e\ufe0f]/;

export function stringSize(str: string): number {
return regexMultiByte.test(str) ? Array.from(str).length : str.length;
}

export function createPadding(length: number, chars: string): string {
const charsLength = stringSize(chars);

if (charsLength === 0 || length < 1) {
return '';
}

const result = chars.repeat(Math.ceil(length / charsLength));

return regexMultiByte.test(result) ? Array.from(result).slice(0, length).join('') : result.slice(0, length);
}
5 changes: 5 additions & 0 deletions src/compat/string/pad.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ describe('pad', () => {
expect(actual).toEqual(expected);
});

it('should treat multi-byte characters as a single code point when padding', () => {
expect(pad('ab', 8, '😀')).toBe('😀😀😀ab😀😀😀');
expect(pad('😀😁😂', 8, '_')).toBe('__😀😁😂___');
});

it('should match the type of lodash', () => {
expectTypeOf(pad).toEqualTypeOf<typeof padLodash>();
});
Expand Down
18 changes: 15 additions & 3 deletions src/compat/string/pad.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pad as padToolkit } from '../../string/pad.ts';
import { createPadding, stringSize } from '../_internal/createPadding.ts';
import { toInteger } from '../util/toInteger.ts';
import { toString } from '../util/toString.ts';

/**
Expand Down Expand Up @@ -35,6 +36,17 @@
* const result4 = pad('abc', 2); // result will be 'abc'
*
*/
export function pad(str: any, length?: any, chars?: any): string {
return padToolkit(toString(str), length, chars);
export function pad(str?: any, length: any = 0, chars: any = ' '): string {

Check warning on line 39 in src/compat/string/pad.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 39 in src/compat/string/pad.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 39 in src/compat/string/pad.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const value = toString(str);
const targetLength = toInteger(length);
const strLength = stringSize(value);

if (targetLength <= strLength) {
return value;
}

const mid = (targetLength - strLength) / 2;
const padChars = `${chars}`;

return createPadding(Math.floor(mid), padChars) + value + createPadding(Math.ceil(mid), padChars);
}
5 changes: 5 additions & 0 deletions src/compat/string/padEnd.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ describe('padEnd', () => {
expect(actual).toEqual(expected);
});

it('should treat multi-byte characters as a single code point when padding', () => {
expect(padEnd('abc', 6, '😀')).toBe('abc😀😀😀');
expect(padEnd('😀😁😂', 8, '_')).toBe('😀😁😂_____');
});

it('should match the type of lodash', () => {
expectTypeOf(padEnd).toEqualTypeOf<typeof padEndLodash>();
});
Expand Down
12 changes: 11 additions & 1 deletion src/compat/string/padEnd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createPadding, stringSize } from '../_internal/createPadding.ts';
import { toInteger } from '../util/toInteger.ts';
import { toString } from '../util/toString.ts';

/**
Expand All @@ -19,5 +21,13 @@ import { toString } from '../util/toString.ts';
*/

export function padEnd(str?: string, length = 0, chars = ' '): string {
return toString(str).padEnd(length, chars);
const value = toString(str);
const targetLength = toInteger(length);
const strLength = stringSize(value);

if (targetLength <= strLength) {
return value;
}

return value + createPadding(targetLength - strLength, `${chars}`);
}
5 changes: 5 additions & 0 deletions src/compat/string/padStart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ describe('padStart', () => {
expect(actual).toEqual(expected);
});

it('should treat multi-byte characters as a single code point when padding', () => {
expect(padStart('abc', 6, '😀')).toBe('😀😀😀abc');
expect(padStart('😀😁😂', 8, '_')).toBe('_____😀😁😂');
});

it('should match the type of lodash', () => {
expectTypeOf(padStart).toEqualTypeOf<typeof padStartLodash>();
});
Expand Down
12 changes: 11 additions & 1 deletion src/compat/string/padStart.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createPadding, stringSize } from '../_internal/createPadding.ts';
import { toInteger } from '../util/toInteger.ts';
import { toString } from '../util/toString.ts';

/**
Expand All @@ -18,5 +20,13 @@ import { toString } from '../util/toString.ts';
* const result4 = padStart('abc', 2); // result will be 'abc'
*/
export function padStart(str?: string, length = 0, chars = ' '): string {
return toString(str).padStart(length, chars);
const value = toString(str);
const targetLength = toInteger(length);
const strLength = stringSize(value);

if (targetLength <= strLength) {
return value;
}

return createPadding(targetLength - strLength, `${chars}`) + value;
}