diff --git a/src/index.js b/src/index.js index e6402e82..4dc863ae 100644 --- a/src/index.js +++ b/src/index.js @@ -4,4 +4,5 @@ export * from './lang' export * from './math' export * from './object' export * from './seq' +export * from './string' export * from './util' diff --git a/src/string/capitalize.js b/src/string/capitalize.js new file mode 100644 index 00000000..b7a0b1de --- /dev/null +++ b/src/string/capitalize.js @@ -0,0 +1,16 @@ +import _capitalize from 'lodash/capitalize' +import { convert } from 'util/convert' + +/** + * Converts the first character of string to upper case and the remaining to lower case. + * @function + * @memberof object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @return {Object} Returns the updated object. + * @example capitalize({ nested: { a: "a string" } }, 'nested.a') // => { nested: { a: "A string" } } + * @see {@link https://lodash.com/docs#capitalize|lodash.capitalize} for more information. + * @since 0.3.0 + */ +const capitalize = convert(_capitalize) +export { capitalize, capitalize as default } diff --git a/src/string/capitalize.spec.js b/src/string/capitalize.spec.js new file mode 100644 index 00000000..9533f9c9 --- /dev/null +++ b/src/string/capitalize.spec.js @@ -0,0 +1,14 @@ +/* eslint-env jest */ +import { capitalize } from './capitalize' +import { immutaTest } from 'test.utils' + +describe('Capitalize', () => { + + it('should convert the first character of string to upper case and the remaining to lower case', () => { + immutaTest((input, path) => { + const output = capitalize(input, path) + expect(output).toEqual({ nested: { prop: 'A string' } }) + return output + }, { nested: { prop: 'a string' } }, 'nested.prop') + }) +}) diff --git a/src/string/index.js b/src/string/index.js new file mode 100644 index 00000000..fe6f80b0 --- /dev/null +++ b/src/string/index.js @@ -0,0 +1,16 @@ +import { capizalize } from './capizalize' +import { replace } from './replace' +import { toLower } from './toLower' +import { toUpper } from './toUpper' + +/** + * String functions. + * @namespace string + * @since 0.3.0 + */ +export { + capizalize, + replace, + toLower, + toUpper, +} diff --git a/src/string/replace.js b/src/string/replace.js new file mode 100644 index 00000000..b2f344bd --- /dev/null +++ b/src/string/replace.js @@ -0,0 +1,18 @@ +import _replace from 'lodash/replace' +import { convert } from 'util/convert' + +/** + * Replaces matches for pattern in string with replacement. + * @function + * @memberof object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @param {RegExp|string} pattern The pattern to replace. + * @param {Function|string} replacement The match replacement. + * @return {Object} Returns the updated object. + * @example replace({ nested: { a: "Hi Nico" } }, 'nested.a', 'Nico', 'Yvo') // => { nested: { a: "Hi Yvo" } } + * @see {@link https://lodash.com/docs#replace|lodash.replace} for more information. + * @since 0.3.0 + */ +const replace = convert(_replace) +export { replace, replace as default } diff --git a/src/string/replace.spec.js b/src/string/replace.spec.js new file mode 100644 index 00000000..68fe55f0 --- /dev/null +++ b/src/string/replace.spec.js @@ -0,0 +1,14 @@ +/* eslint-env jest */ +import { immutaTest } from 'test.utils' +import { replace } from './replace' + +describe('Replace', () => { + + it('should replace matches for pattern in string with replacement', () => { + immutaTest((input, path) => { + const output = replace(input, path, 'Nico', 'Yvo') + expect(output).toEqual({ nested: { prop: 'Hi Yvo' } }) + return output + }, { nested: { prop: 'Hi Nico' } }, 'nested.prop') + }) +}) diff --git a/src/string/toLower.js b/src/string/toLower.js new file mode 100644 index 00000000..fee15102 --- /dev/null +++ b/src/string/toLower.js @@ -0,0 +1,17 @@ +import _toLower from 'lodash/toLower' +import { convert } from 'util/convert' + +/** + * Converts string, as a whole, to lower case just like String#toLowerCase. + * @function + * @memberof object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @return {Object} Returns the updated object. + * @example toLower({ nested: { a: "A STRING" } }, 'nested.a') // => { nested: { a: "a string" } } + * @see {@link https://lodash.com/docs#toLower|lodash.toLower} for more information. + * @see {@link https://mdn.io/String/toLowerCase|String.toLowerCase} for more information. + * @since 0.3.0 + */ +const toLower = convert(_toLower) +export { toLower, toLower as default } diff --git a/src/string/toLower.spec.js b/src/string/toLower.spec.js new file mode 100644 index 00000000..9016ab30 --- /dev/null +++ b/src/string/toLower.spec.js @@ -0,0 +1,14 @@ +/* eslint-env jest */ +import { immutaTest } from 'test.utils' +import { toLower } from './toLower' + +describe('toLower', () => { + + it('should convert string, as a whole, to lower case just like String#toLowerCase', () => { + immutaTest((input, path) => { + const output = toLower(input, path) + expect(output).toEqual({ nested: { prop: 'a string' } }) + return output + }, { nested: { prop: 'A STRING' } }, 'nested.prop') + }) +}) diff --git a/src/string/toUpper.js b/src/string/toUpper.js new file mode 100644 index 00000000..1d8272af --- /dev/null +++ b/src/string/toUpper.js @@ -0,0 +1,17 @@ +import _toUpper from 'lodash/toUpper' +import { convert } from 'util/convert' + +/** + * Converts string, as a whole, to upper case just like String#toUpperCase. + * @function + * @memberof object + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @return {Object} Returns the updated object. + * @example toUpper({ nested: { a: "a string" } }, 'nested.a') // => { nested: { a: "A STRING" } } + * @see {@link https://lodash.com/docs#toUpper|lodash.toUpper} for more information. + * @see {@link https://mdn.io/String/toUpperCase|String.toUpperCase} for more information. + * @since 0.3.0 + */ +const toUpper = convert(_toUpper) +export { toUpper, toUpper as default } diff --git a/src/string/toUpper.spec.js b/src/string/toUpper.spec.js new file mode 100644 index 00000000..e746df4f --- /dev/null +++ b/src/string/toUpper.spec.js @@ -0,0 +1,14 @@ +/* eslint-env jest */ +import { immutaTest } from 'test.utils' +import { toUpper } from './toUpper' + +describe('toUpper', () => { + + it('should convert string, as a whole, to upper case just like String#toUpperCase', () => { + immutaTest((input, path) => { + const output = toUpper(input, path) + expect(output).toEqual({ nested: { prop: 'A STRING' } }) + return output + }, { nested: { prop: 'a string' } }, 'nested.prop') + }) +})