From 14cfdab0018adbcd5d08f4280dea0b1075858790 Mon Sep 17 00:00:00 2001 From: nlepage <19571875+nlepage@users.noreply.github.com> Date: Thu, 14 Dec 2017 14:01:55 +0100 Subject: [PATCH] :sparkles: Add string.slice --- packages/immutadot/src/string/index.js | 1 + packages/immutadot/src/string/slice.js | 20 +++++++++++++++++++ packages/immutadot/src/string/slice.spec.js | 22 +++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 packages/immutadot/src/string/slice.js create mode 100644 packages/immutadot/src/string/slice.spec.js diff --git a/packages/immutadot/src/string/index.js b/packages/immutadot/src/string/index.js index 2f18f913..7963a1af 100644 --- a/packages/immutadot/src/string/index.js +++ b/packages/immutadot/src/string/index.js @@ -8,3 +8,4 @@ export { concat } from './concat' export { padEnd } from './padEnd' export { padStart } from './padStart' export { replace } from './replace' +export { slice } from './slice' diff --git a/packages/immutadot/src/string/slice.js b/packages/immutadot/src/string/slice.js new file mode 100644 index 00000000..29eb5063 --- /dev/null +++ b/packages/immutadot/src/string/slice.js @@ -0,0 +1,20 @@ +import { convertStringMethod } from './convertStringMethod' + +/** + * Replaces by a slice of former string starting at beginIndex and ending at endIndex or end of the string. + * @function + * @memberof string + * @param {Object} object The object to modify. + * @param {Array|string} path The path of the property to set. + * @param {number} beginIndex Beginning index of slice. + * @param {number?} endIndex Ending index of slice. + * @return {Object} Returns the updated object. + * @example slice({ nested: { a: 'Hello World !' } }, 6) // => { nested: { a: 'World !' } } + * @example slice({ nested: { a: 'Hello World !' } }, 6, 11) // => { nested: { a: 'World' } } + * @see {@link https://mdn.io/String.prototype.slice|String.prototype.slice} for more information. + * @since 1.0.0 + * @flow + */ +const slice = convertStringMethod('slice') + +export { slice } diff --git a/packages/immutadot/src/string/slice.spec.js b/packages/immutadot/src/string/slice.spec.js new file mode 100644 index 00000000..3084af3b --- /dev/null +++ b/packages/immutadot/src/string/slice.spec.js @@ -0,0 +1,22 @@ +/* eslint-env jest */ +import { immutaTest } from 'test.utils' +import { slice } from 'string' + +describe('string.slice', () => { + + it('should return a slice to the end', () => { + immutaTest((input, path) => { + const output = slice(input, path, 6) + expect(output).toEqual({ nested: { prop: 'World !' } }) + return output + }, { nested: { prop: 'Hello World !' } }, 'nested.prop') + }) + + it('should return a slice to the end', () => { + immutaTest((input, path) => { + const output = slice(input, path, 6, 11) + expect(output).toEqual({ nested: { prop: 'World' } }) + return output + }, { nested: { prop: 'Hello World !' } }, 'nested.prop') + }) +})