diff --git a/packages/util/package.json b/packages/util/package.json index 7c7b175cc..f9c0217f9 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -30,10 +30,12 @@ "dependencies": { "@babel/runtime": "^7.0.0", "@types/bn.js": "^4.11.1", + "@types/camelcase": "^4.1.0", "@types/deasync": "^0.1.0", "@types/ip-regex": "^2.0.0", "@types/xxhashjs": "^0.1.1", "bn.js": "^4.11.8", + "camelcase": "^5.0.0", "chalk": "^2.4.1", "deasync": "^0.1.13", "ip-regex": "^3.0.0", diff --git a/packages/util/src/string/camelCase.ts b/packages/util/src/string/camelCase.ts new file mode 100644 index 000000000..fa84c0601 --- /dev/null +++ b/packages/util/src/string/camelCase.ts @@ -0,0 +1,7 @@ +// Copyright 2017-2018 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import * as stringCamelCase from 'camelcase'; + +export default stringCamelCase; diff --git a/packages/util/src/string/index.ts b/packages/util/src/string/index.ts index 487c61c6e..aa7293130 100644 --- a/packages/util/src/string/index.ts +++ b/packages/util/src/string/index.ts @@ -2,11 +2,17 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. +import stringCamelCase from './camelCase'; +import stringLowerFirst from './lowerFirst'; import stringShorten from './shorten'; +import stringUpperFirst from './upperFirst'; /** * @summary Utility methods to convert to work with `string` values */ export { - stringShorten + stringCamelCase, + stringLowerFirst, + stringShorten, + stringUpperFirst }; diff --git a/packages/util/src/string/lowerFirst.spec.js b/packages/util/src/string/lowerFirst.spec.js new file mode 100644 index 000000000..a83627c52 --- /dev/null +++ b/packages/util/src/string/lowerFirst.spec.js @@ -0,0 +1,31 @@ +// Copyright 2017-2018 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import { stringLowerFirst } from './index'; + +describe('stringLowerFirst', () => { + it("lowers the first letter if it's a capital letter", () => { + expect( + stringLowerFirst('ABC') + ).toBe('aBC'); + }); + + it("lowers the first letter if it's a lowercase letter", () => { + expect( + stringLowerFirst('abc') + ).toBe('abc'); + }); + + it('returns undefined as undefined', () => { + expect( + stringLowerFirst() + ).toBe(undefined); + }); + + it('returns null as null', () => { + expect( + stringLowerFirst(null) + ).toBe(null); + }); +}); diff --git a/packages/util/src/string/lowerFirst.ts b/packages/util/src/string/lowerFirst.ts new file mode 100644 index 000000000..8e65ae58f --- /dev/null +++ b/packages/util/src/string/lowerFirst.ts @@ -0,0 +1,25 @@ +// Copyright 2017-2018 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import isNull from '../is/null'; +import isUndefined from '../is/undefined'; + +/** + * @name stringLowerFirst + * @signature stringLowerFirst (_value: string): string + * @summary Lowercase the first letter of a string + * @description + * Lowercase the first letter of a string + * @example + * import { stringLowerFirst } from '@polkadot/util'; + * + * stringLowerFirst('ABC'); // => 'aBC' + */ +export default function stringLowerFirst (_value: string): string { + if (isNull(_value) || isUndefined(_value)) { + return _value; + } + + return _value.charAt(0).toLowerCase() + _value.slice(1); +} diff --git a/packages/util/src/string/upperFirst.spec.js b/packages/util/src/string/upperFirst.spec.js new file mode 100644 index 000000000..3996349df --- /dev/null +++ b/packages/util/src/string/upperFirst.spec.js @@ -0,0 +1,31 @@ +// Copyright 2017-2018 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import { stringUpperFirst } from '.'; + +describe('stringUpperFirst', () => { + it("uppers the first letter if it's a capital letter", () => { + expect( + stringUpperFirst('ABC') + ).toBe('ABC'); + }); + + it("uppers the first letter if it's a lowercase letter", () => { + expect( + stringUpperFirst('abc') + ).toBe('Abc'); + }); + + it('returns undefined as undefined', () => { + expect( + stringUpperFirst() + ).toBe(undefined); + }); + + it('returns null as null', () => { + expect( + stringUpperFirst(null) + ).toBe(null); + }); +}); diff --git a/packages/util/src/string/upperFirst.ts b/packages/util/src/string/upperFirst.ts new file mode 100644 index 000000000..e2e81c456 --- /dev/null +++ b/packages/util/src/string/upperFirst.ts @@ -0,0 +1,25 @@ +// Copyright 2017-2018 @polkadot/util authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import isNull from '../is/null'; +import isUndefined from '../is/undefined'; + +/** + * @name stringUpperFirst + * @signature stringUpperFirst (_value: string): string + * @summary Lowercase the first letter of a string + * @description + * Lowercase the first letter of a string + * @example + * import { stringUpperFirst } from '@polkadot/util'; + * + * stringUpperFirst('abc'); // => 'Abc' + */ +export default function stringUpperFirst (_value: string): string { + if (isNull(_value) || isUndefined(_value)) { + return _value; + } + + return _value.charAt(0).toUpperCase() + _value.slice(1); +} diff --git a/yarn.lock b/yarn.lock index 1eb22695a..cd83b57fd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1233,6 +1233,10 @@ dependencies: "@types/base-x" "*" +"@types/camelcase@^4.1.0": + version "4.1.0" + resolved "https://registry.yarnpkg.com/@types/camelcase/-/camelcase-4.1.0.tgz#e054f7986f31658d49936261b5cd4588ef29d1ee" + "@types/deasync@^0.1.0": version "0.1.0" resolved "https://registry.yarnpkg.com/@types/deasync/-/deasync-0.1.0.tgz#b2bd6c3fcde3cf67b8be4c2f70136ba8f157b45a" @@ -1663,7 +1667,31 @@ babel-code-frame@^6.22.0, babel-code-frame@^6.26.0: esutils "^2.0.2" js-tokens "^3.0.2" -babel-core@^6.0.0, babel-core@^7.0.0-bridge.0: +babel-core@^6.0.0, babel-core@^6.26.0: + version "6.26.3" + resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.26.3.tgz#b2e2f09e342d0f0c88e2f02e067794125e75c207" + dependencies: + babel-code-frame "^6.26.0" + babel-generator "^6.26.0" + babel-helpers "^6.24.1" + babel-messages "^6.23.0" + babel-register "^6.26.0" + babel-runtime "^6.26.0" + babel-template "^6.26.0" + babel-traverse "^6.26.0" + babel-types "^6.26.0" + babylon "^6.18.0" + convert-source-map "^1.5.1" + debug "^2.6.9" + json5 "^0.5.1" + lodash "^4.17.4" + minimatch "^3.0.4" + path-is-absolute "^1.0.1" + private "^0.1.8" + slash "^1.0.0" + source-map "^0.5.7" + +babel-core@^7.0.0-bridge.0: version "7.0.0-bridge.0" resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-7.0.0-bridge.0.tgz#95a492ddd90f9b4e9a4a1da14eb335b87b634ece" @@ -1691,6 +1719,26 @@ babel-generator@^6.18.0: source-map "^0.5.6" trim-right "^1.0.1" +babel-generator@^6.26.0: + version "6.26.1" + resolved "https://registry.yarnpkg.com/babel-generator/-/babel-generator-6.26.1.tgz#1844408d3b8f0d35a404ea7ac180f087a601bd90" + dependencies: + babel-messages "^6.23.0" + babel-runtime "^6.26.0" + babel-types "^6.26.0" + detect-indent "^4.0.0" + jsesc "^1.3.0" + lodash "^4.17.4" + source-map "^0.5.7" + trim-right "^1.0.1" + +babel-helpers@^6.24.1: + version "6.24.1" + resolved "https://registry.yarnpkg.com/babel-helpers/-/babel-helpers-6.24.1.tgz#3471de9caec388e5c850e597e58a26ddf37602b2" + dependencies: + babel-runtime "^6.22.0" + babel-template "^6.24.1" + babel-jest@^23.4.2: version "23.4.2" resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-23.4.2.tgz#f276de67798a5d68f2d6e87ff518c2f6e1609877" @@ -1728,6 +1776,18 @@ babel-preset-jest@^23.2.0: babel-plugin-jest-hoist "^23.2.0" babel-plugin-syntax-object-rest-spread "^6.13.0" +babel-register@^6.26.0: + version "6.26.0" + resolved "https://registry.yarnpkg.com/babel-register/-/babel-register-6.26.0.tgz#6ed021173e2fcb486d7acb45c6009a856f647071" + dependencies: + babel-core "^6.26.0" + babel-runtime "^6.26.0" + core-js "^2.5.0" + home-or-tmp "^2.0.0" + lodash "^4.17.4" + mkdirp "^0.5.1" + source-map-support "^0.4.15" + babel-runtime@^6.22.0, babel-runtime@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe" @@ -1735,7 +1795,7 @@ babel-runtime@^6.22.0, babel-runtime@^6.26.0: core-js "^2.4.0" regenerator-runtime "^0.11.0" -babel-template@^6.16.0: +babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.26.0.tgz#de03e2d16396b069f46dd9fff8521fb1a0e35e02" dependencies: @@ -2120,6 +2180,10 @@ camelcase@^4.0.0, camelcase@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" +camelcase@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.0.0.tgz#03295527d58bd3cd4aa75363f35b2e8d97be2f42" + caniuse-lite@^1.0.30000884: version "1.0.30000885" resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000885.tgz#e889e9f8e7e50e769f2a49634c932b8aee622984" @@ -2501,6 +2565,12 @@ convert-source-map@^1.1.0, convert-source-map@^1.4.0: version "1.5.1" resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.5.1.tgz#b8278097b9bc229365de5c62cf5fcaed8b5599e5" +convert-source-map@^1.5.1: + version "1.6.0" + resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.6.0.tgz#51b537a8c43e0f04dec1993bffcdd504e758ac20" + dependencies: + safe-buffer "~5.1.1" + copy-concurrently@^1.0.0: version "1.0.5" resolved "https://registry.yarnpkg.com/copy-concurrently/-/copy-concurrently-1.0.5.tgz#92297398cae34937fcafd6ec8139c18051f0b5e0" @@ -2531,7 +2601,7 @@ core-js@^2.4.0: version "2.5.1" resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.1.tgz#ae6874dc66937789b80754ff5428df66819ca50b" -core-js@^2.5.7: +core-js@^2.5.0, core-js@^2.5.7: version "2.5.7" resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.5.7.tgz#f972608ff0cead68b841a16a932d0b183791814e" @@ -2668,7 +2738,7 @@ debug@3.1.0, debug@^3.1.0: dependencies: ms "2.0.0" -debug@^2.1.2, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8: +debug@^2.1.2, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" dependencies: @@ -4020,6 +4090,13 @@ hoek@4.x.x: version "4.2.0" resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.0.tgz#72d9d0754f7fe25ca2d01ad8f8f9a9449a89526d" +home-or-tmp@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8" + dependencies: + os-homedir "^1.0.0" + os-tmpdir "^1.0.1" + home-or-tmp@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-3.0.0.tgz#57a8fe24cf33cdd524860a15821ddc25c86671fb" @@ -5056,7 +5133,7 @@ json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" -json5@^0.5.0: +json5@^0.5.0, json5@^0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821" @@ -6379,7 +6456,7 @@ os-locale@^2.0.0: lcid "^1.0.0" mem "^1.1.0" -os-tmpdir@^1.0.0, os-tmpdir@~1.0.1, os-tmpdir@~1.0.2: +os-tmpdir@^1.0.0, os-tmpdir@^1.0.1, os-tmpdir@~1.0.1, os-tmpdir@~1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274" @@ -6588,7 +6665,7 @@ path-exists@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515" -path-is-absolute@^1.0.0: +path-is-absolute@^1.0.0, path-is-absolute@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" @@ -6703,7 +6780,7 @@ pretty-format@^23.5.0: ansi-regex "^3.0.0" ansi-styles "^3.2.0" -private@^0.1.6: +private@^0.1.6, private@^0.1.8: version "0.1.8" resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff" @@ -7712,6 +7789,12 @@ source-map-resolve@^0.5.0: source-map-url "^0.4.0" urix "^0.1.0" +source-map-support@^0.4.15: + version "0.4.18" + resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.18.tgz#0286a6de8be42641338594e97ccea75f0a2c585f" + dependencies: + source-map "^0.5.6" + source-map-support@^0.5.6: version "0.5.6" resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.6.tgz#4435cee46b1aab62b8e8610ce60f788091c51c13" @@ -7736,7 +7819,7 @@ source-map@^0.4.4: dependencies: amdefine ">=0.0.4" -source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6, source-map@~0.5.1, source-map@~0.5.6: +source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6, source-map@^0.5.7, source-map@~0.5.1, source-map@~0.5.6: version "0.5.7" resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc"