string* takes in String (class) inputs (#732)

This commit is contained in:
Jaco Greeff
2020-10-28 13:52:40 +01:00
committed by GitHub
parent bf28507ae8
commit b51ed71796
13 changed files with 63 additions and 13 deletions
@@ -9,4 +9,10 @@ describe('stringCamelCase', (): void => {
stringCamelCase('Snake_case')
).toBe('snakeCase');
});
it('works correctly for String (class', (): void => {
expect(
stringCamelCase(String('Snake_case'))
).toBe('snakeCase');
});
});
+9 -2
View File
@@ -1,6 +1,13 @@
// Copyright 2017-2020 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import stringCamelCase from 'camelcase';
import camelcase from 'camelcase';
export default stringCamelCase;
/**
* @name stringCamelCase
* @summary Convert a dash/dot/underscore/space separated string/String to camelCase
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export default function stringCamelCase (value: string | String): string {
return camelcase(value.toString());
}
@@ -10,6 +10,12 @@ describe('stringLowerFirst', (): void => {
).toBe('aBC');
});
it("lowers the first letter if it's a capital letter (String)", (): void => {
expect(
stringLowerFirst(String('ABC'))
).toBe('aBC');
});
it("lowers the first letter if it's a lowercase letter", (): void => {
expect(
stringLowerFirst('abc')
+2 -1
View File
@@ -15,7 +15,8 @@
* stringLowerFirst('ABC'); // => 'aBC'
* ```
*/
export default function stringLowerFirst (value?: string | null): string {
// eslint-disable-next-line @typescript-eslint/ban-types
export default function stringLowerFirst (value?: string | String | null): string {
return value
? value.charAt(0).toLowerCase() + value.slice(1)
: '';
+6
View File
@@ -15,4 +15,10 @@ describe('stringShorten', (): void => {
stringShorten('0123456789', 3)
).toEqual('012…789');
});
it('returns the shortened value when > maxLength (String)', (): void => {
expect(
stringShorten(String('0123456789'), 3)
).toEqual('012…789');
});
});
+3 -3
View File
@@ -15,10 +15,10 @@
* stringShorten('1234567890', 2); // => 12..90
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function stringShorten (value: string, prefixLength = 6): string {
// eslint-disable-next-line @typescript-eslint/ban-types
export default function stringShorten (value: String | string, prefixLength = 6): string {
if (value.length <= 2 + 2 * prefixLength) {
return value;
return value.toString();
}
return `${value.substr(0, prefixLength)}…${value.slice(-prefixLength)}`;
+6
View File
@@ -15,4 +15,10 @@ describe('hexToString', (): void => {
stringToHex('hello')
).toEqual('0x68656c6c6f');
});
it('converts to a hex from String', (): void => {
expect(
stringToHex(String('hello'))
).toEqual('0x68656c6c6f');
});
});
+2 -1
View File
@@ -18,7 +18,8 @@ import stringToU8a from './toU8a';
* stringToU8a('hello'); // 0x68656c6c6f
* ```
*/
export default function stringToHex (value?: string): string {
// eslint-disable-next-line @typescript-eslint/ban-types
export default function stringToHex (value?: String | string): string {
return u8aToHex(
stringToU8a(value)
);
+9 -3
View File
@@ -3,6 +3,8 @@
import { stringToU8a } from '..';
const RUSS_HELLO = new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33]);
describe('stringToU8a', (): void => {
it('decodes to an empty string for undefined', (): void => {
expect(
@@ -13,8 +15,12 @@ describe('stringToU8a', (): void => {
it('encodes the string correctly', (): void => {
expect(
stringToU8a('Привет, мир!')
).toEqual(
new Uint8Array([208, 159, 209, 128, 208, 184, 208, 178, 208, 181, 209, 130, 44, 32, 208, 188, 208, 184, 209, 128, 33])
);
).toEqual(RUSS_HELLO);
});
it('encodes the string correctly (String)', (): void => {
expect(
stringToU8a(String('Привет, мир!'))
).toEqual(RUSS_HELLO);
});
});
+3 -2
View File
@@ -19,8 +19,9 @@ const encoder = new TextEncoder();
* stringToU8a('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
* ```
*/
export default function stringToU8a (value?: string): Uint8Array {
// eslint-disable-next-line @typescript-eslint/ban-types
export default function stringToU8a (value?: String | string): Uint8Array {
return value
? encoder.encode(value)
? encoder.encode(value.toString())
: new Uint8Array();
}
@@ -16,6 +16,12 @@ describe('stringUpperFirst', (): void => {
).toBe('Abc');
});
it("uppers the first letter if it's a lowercase letter (String)", (): void => {
expect(
stringUpperFirst(String('abc'))
).toBe('Abc');
});
it('returns undefined as empty', (): void => {
expect(
stringUpperFirst()
+2 -1
View File
@@ -15,7 +15,8 @@
* stringUpperFirst('abc'); // => 'Abc'
* ```
*/
export default function stringUpperFirst (value?: string | null): string {
// eslint-disable-next-line @typescript-eslint/ban-types
export default function stringUpperFirst (value?: String | string | null): string {
return value
? value.charAt(0).toUpperCase() + value.slice(1)
: '';