Skip to content

Commit

Permalink
✨ Add string.slice
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Dec 14, 2017
1 parent 8db4fd8 commit 14cfdab
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/immutadot/src/string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { concat } from './concat'
export { padEnd } from './padEnd'
export { padStart } from './padStart'
export { replace } from './replace'
export { slice } from './slice'
20 changes: 20 additions & 0 deletions packages/immutadot/src/string/slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { convertStringMethod } from './convertStringMethod'

/**
* Replaces by a slice of former string starting at <code>beginIndex</code> and ending at <code>endIndex</code> 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 }
22 changes: 22 additions & 0 deletions packages/immutadot/src/string/slice.spec.js
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit 14cfdab

Please sign in to comment.