Small consistency cleanups (#1178)

* Small consistency cleanups

* Adjust

* Adjust
This commit is contained in:
Jaco
2021-11-13 17:39:52 +02:00
committed by GitHub
parent 9043f335d4
commit 5fdd967666
14 changed files with 48 additions and 31 deletions
+3 -2
View File
@@ -1,13 +1,14 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
import camelcase from 'camelcase';
/**
* @name stringCamelCase
* @summary Convert a dash/dot/underscore/space separated string/String to camelCase
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringCamelCase (value: string | String): string {
export function stringCamelCase (value: AnyString): string {
return camelcase(value.toString());
}
+3 -2
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
/**
* @name stringLowerFirst
* @summary Lowercase the first letter of a string
@@ -15,8 +17,7 @@
* stringLowerFirst('ABC'); // => 'aBC'
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringLowerFirst (value?: string | String | null): string {
export function stringLowerFirst (value?: AnyString | null): string {
return value
? value.charAt(0).toLowerCase() + value.slice(1)
: '';
+6 -3
View File
@@ -1,13 +1,16 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
import camelcase from 'camelcase';
const OPTS = { pascalCase: true };
/**
* @name stringPascalCase
* @summary Convert a dash/dot/underscore/space separated string/String to PascalCase
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringPascalCase (value: string | String): string {
return camelcase(value.toString(), { pascalCase: true });
export function stringPascalCase (value: AnyString): string {
return camelcase(value.toString(), OPTS);
}
+3 -2
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
/**
* @name stringShorten
* @summary Returns a string with maximum length
@@ -15,8 +17,7 @@
* stringShorten('1234567890', 2); // => 12..90
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringShorten (value: String | string, prefixLength = 6): string {
export function stringShorten (value: AnyString, prefixLength = 6): string {
if (value.length <= 2 + 2 * prefixLength) {
return value.toString();
}
+2 -3
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '../types';
import type { AnyString, HexString } from '../types';
import { u8aToHex } from '../u8a/toHex';
import { stringToU8a } from './toU8a';
@@ -20,8 +20,7 @@ import { stringToU8a } from './toU8a';
* stringToU8a('hello'); // 0x68656c6c6f
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringToHex (value?: String | string): HexString {
export function stringToHex (value?: AnyString): HexString {
return u8aToHex(
stringToU8a(value)
);
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
import { TextEncoder } from '@polkadot/x-textencoder';
const encoder = new TextEncoder();
@@ -20,7 +22,7 @@ const encoder = new TextEncoder();
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringToU8a (value?: String | string): Uint8Array {
export function stringToU8a (value?: AnyString): Uint8Array {
return value
? encoder.encode(value.toString())
: new Uint8Array();
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
/**
* @name stringUpperFirst
* @summary Lowercase the first letter of a string
@@ -16,7 +18,7 @@
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringUpperFirst (value?: String | string | null): string {
export function stringUpperFirst (value?: AnyString | null): string {
return value
? value.charAt(0).toUpperCase() + value.slice(1)
: '';