Skip to content

Commit

Permalink
✨ Add core functions (#165)
Browse files Browse the repository at this point in the history
* ✨ Add array.filter

* ✨ Add array.map

* ✨ Add array.pop

* 💡 Add array.map JSDoc

* ✨ Add array.shift

* ✨ Add array.sort

* 💡 Update array functions JSDoc

* 🚚 Move math functions to lang

* ✅ Update array tests titles

* ✅ Update tests titles to have namespace

* 🔥 Remove set, update and unset aliases from object

* ♻️ Simplify namespaces indexes

* ♻️ Run number functions operands into Number

* 💡 Replace/remove lodash mentions

* ✨ Add string.concat

* ♻️ Use convertStringMethod in string.replace

* 🐛 Avoid name conflicts in flow

* 🚨 fix linter error in flow generated files

* 🚨 fix linter error in flow generated files

* ✨ Add string.padEnd

* ✨ Add string.padStart

* ✨ Add string.slice

* ✨ Add string.substr

* ✨ Add string.substring

* ✨ Add toLocaleLowerCase

* ✨ Add string.toLocaleUpperCase

* ✨ Add string.toLowerCase and string.toUpperCase

* ✨ Add string.trim

* ✨ Add string.trimLeft and string.trimRight

* ✅ fix tests
  • Loading branch information
nlepage committed Dec 15, 2017
1 parent 341c934 commit c1caf7c
Show file tree
Hide file tree
Showing 95 changed files with 904 additions and 185 deletions.
23 changes: 18 additions & 5 deletions misc/generate-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ const generateFlow = async () => {

const nsItems = itemsByNamespace[namespace]

nsItems.forEach(async ({ name }) => await writeFile(
path.resolve(nsDir, `${name}.js`),
`import { ${name} } from '${namespace}/${name}'
await (async () => {
for (const { name } of nsItems) {
await writeFile(
path.resolve(nsDir, `${name}.js`),
`import { ${name} } from '${namespace}/${name}'
const { curried } = ${name}
export { curried as ${name} }
`,
))
)
}
})()

await writeFile(
path.resolve(nsDir, 'index.js'),
Expand All @@ -51,10 +55,19 @@ export { curried as ${name} }
)
}))

const exportedNames = new Set()
await writeFile(
path.resolve(flowDir, 'exports.js'),
`${namespaces.map(namespace => `export * from './${namespace}'`).join('\n')}
`${namespaces.map(namespace => {
const nsItems = itemsByNamespace[namespace].filter(({ name }) => !exportedNames.has(name))
nsItems.forEach(({ name }) => exportedNames.add(name))
/* eslint-disable */
return `export {
${nsItems.map(({ name }) => ` ${name},`).join('\n')}
} from './${namespace}'`
}).join('\n\n')}
`,
/* eslint-enable */
)
} catch (e) {
console.error(e) // eslint-disable-line no-console
Expand Down
5 changes: 3 additions & 2 deletions packages/immutadot/src/array/concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { convertArrayMethod } from './convertArrayMethod'
* @param {...Array} arrays The arrays to concatenate.
* @return {Object} Returns the updated object.
* @example concat({ nested: { prop: [1, 2] } }, 'nested.prop', [3, 4]) // => { nested: { prop: [1, 2, 3, 4] } }
* @see {@link https://lodash.com/docs#concat|lodash.concat} for more information.
* @see {@link https://mdn.io/Array.prototype.concat|Array.prototype.concat} for more information.
* @since 0.2.0
* @flow
*/
const concat = convertArrayMethod('concat', false, true)
const concat = convertArrayMethod('concat', false)

export { concat }
2 changes: 1 addition & 1 deletion packages/immutadot/src/array/concat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { concat } from 'array'
import { immutaTest } from 'test.utils'

describe('Concat', () => {
describe('array.concat', () => {

it('should concat an array', () => {
immutaTest((input, path) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/immutadot/src/array/fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { convertArrayMethod } from './convertArrayMethod'
* @param {number} [end=array.length]
* @return {Object} Returns the updated object.
* @example fill({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', 6, 1, 3) // => { nested: { prop: [1, 6, 6, 4] } }
* @see {@link https://lodash.com/docs#fill|lodash.fill} for more information.
* @see {@link https://mdn.io/Array.prototype.fill|Array.prototype.fill} for more information.
* @since 0.3.0
* @flow
*/
const fill = convertArrayMethod('fill')

export { fill }
2 changes: 1 addition & 1 deletion packages/immutadot/src/array/fill.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { fill } from 'array'
import { immutaTest } from 'test.utils'

describe('Fill', () => {
describe('array.fill', () => {

it('should fill array with 6 from 1 to 3 excluded', () => {
immutaTest((input, path) => {
Expand Down
18 changes: 18 additions & 0 deletions packages/immutadot/src/array/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { convertArrayMethod } from './convertArrayMethod'

/**
* Replaces by an array of elements <code>predicate</code> returns truthy for.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {function} predicate The function invoked per iteration.
* @return {Object} Returns the updated object.
* @example filter({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop', v => v % 2) // => { nested: { prop: [1, 3] } }
* @see {@link https://mdn.io/Array.prototype.filter|Array.prototype.filter} for more information.
* @since 1.0.0
* @flow
*/
const filter = convertArrayMethod('filter', false)

export { filter }
36 changes: 36 additions & 0 deletions packages/immutadot/src/array/filter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-env jest */
import { filter } from 'array'
import { immutaTest } from 'test.utils'

describe('array.filter', () => {

it('should filter elements', () => {
immutaTest((input, path) => {
const output = filter(input, path, v => v % 2 === 0)
expect(output).toEqual({
nested: { prop: [2, 4] },
other: {},
})
return output
}, {
nested: { prop: [1, 2, 3, 4] },
other: {},
}, 'nested.prop')
})

it('should replace deep undefined with array', () => {
immutaTest((input, path) => {
const output = filter(input, path, () => true)
expect(output).toEqual({ nested: { prop: [] } })
return output
}, undefined, 'nested.prop')
})

it('should wrap value in an array', () => {
immutaTest((input, path) => {
const output = filter(input, path, () => true)
expect(output).toEqual({ nested: { prop: [2] } })
return output
}, { nested: { prop: 2 } }, 'nested.prop')
})
})
30 changes: 13 additions & 17 deletions packages/immutadot/src/array/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { concat } from './concat'
import { fill } from './fill'
import { push } from './push'
import { reverse } from './reverse'
import { slice } from './slice'
import { splice } from './splice'
import { unshift } from './unshift'

/**
* Array functions.
* @namespace array
* @since 0.1.6
*/
export {
concat,
fill,
push,
reverse,
slice,
splice,
unshift,
}

export { filter } from './filter'
export { concat } from './concat'
export { fill } from './fill'
export { map } from './map'
export { pop } from './pop'
export { push } from './push'
export { reverse } from './reverse'
export { shift } from './shift'
export { slice } from './slice'
export { splice } from './splice'
export { sort } from './sort'
export { unshift } from './unshift'
18 changes: 18 additions & 0 deletions packages/immutadot/src/array/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { convertArrayMethod } from './convertArrayMethod'

/**
* Replaces by an array of values by running each element in the former collection thru callback.
* @function
* @memberof array
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to set.
* @param {function} callback The function invoked per iteration.
* @return {Object} Returns the updated object.
* @example map({ nested: { prop: [1, 2, 3] } }, 'nested.prop', v => v * 2) // => { nested: { prop: [2, 4, 6] } }
* @see {@link https://mdn.io/Array.prototype.map|Array.prototype.map} for more information.
* @since 1.0.0
* @flow
*/
const map = convertArrayMethod('map', false)

export { map }
36 changes: 36 additions & 0 deletions packages/immutadot/src/array/map.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-env jest */
import { immutaTest } from 'test.utils'
import { map } from 'array'

describe('array.map', () => {

it('should map elements', () => {
immutaTest((input, path) => {
const output = map(input, path, v => v * v)
expect(output).toEqual({
nested: { prop: [1, 4, 9, 16] },
other: {},
})
return output
}, {
nested: { prop: [1, 2, 3, 4] },
other: {},
}, 'nested.prop')
})

it('should replace deep undefined with array', () => {
immutaTest((input, path) => {
const output = map(input, path, v => v)
expect(output).toEqual({ nested: { prop: [] } })
return output
}, undefined, 'nested.prop')
})

it('should wrap value in an array', () => {
immutaTest((input, path) => {
const output = map(input, path, v => v * v)
expect(output).toEqual({ nested: { prop: [4] } })
return output
}, { nested: { prop: 2 } }, 'nested.prop')
})
})
17 changes: 17 additions & 0 deletions packages/immutadot/src/array/pop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { convertArrayMethod } from './convertArrayMethod'

/**
* Replaces by an array of elements with last element removed.
* @function
* @memberof array
* @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 pop({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop') // => { nested: { prop: [1, 2, 3] } }
* @see {@link https://mdn.io/Array.prototype.pop|Array.prototype.pop} for more information.
* @since 1.0.0
* @flow
*/
const pop = convertArrayMethod('pop')

export { pop }
42 changes: 42 additions & 0 deletions packages/immutadot/src/array/pop.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-env jest */
import { immutaTest } from 'test.utils'
import { pop } from 'array'

describe('array.pop', () => {

it('should remove last element', () => {
immutaTest((input, path) => {
const output = pop(input, path)
expect(output).toEqual({
nested: { prop: [1, 2, 3] },
other: {},
})
return output
}, {
nested: { prop: [1, 2, 3, 4] },
other: {},
}, 'nested.prop')
})

it('should replace deep undefined with array', () => {
immutaTest((input, path) => {
const output = pop(input, path, () => true)
expect(output).toEqual({ nested: { prop: [] } })
return output
}, undefined, 'nested.prop')
})

it('should wrap value in array and remove it', () => {
immutaTest((input, path) => {
const output = pop(input, path)
expect(output).toEqual({
nested: { prop: [] },
other: {},
})
return output
}, {
nested: { prop: 123 },
other: {},
}, 'nested.prop')
})
})
1 change: 1 addition & 0 deletions packages/immutadot/src/array/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ import { convertArrayMethod } from './convertArrayMethod'
* @flow
*/
const push = convertArrayMethod('push')

export { push }
2 changes: 1 addition & 1 deletion packages/immutadot/src/array/push.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { immutaTest } from 'test.utils'
import { push } from 'array'

describe('Push', () => {
describe('array.push', () => {

it('should add one element', () => {
immutaTest((input, path) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/immutadot/src/array/reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { convertArrayMethod } from './convertArrayMethod'
* @param {Array|string} path The path of the property to set.
* @return {Object} Returns the updated object.
* @example reverse({ nested: { prop: [1, 2, 3] } }, 'nested.prop') // => { nested: { prop: [3, 2, 1] } }
* @see {@link https://lodash.com/docs#reverse|lodash.reverse} for more information.
* @see {@link https://mdn.io/Array.prototype.reverse|Array.prototype.reverse} for more information..
* @since 0.3.0
* @flow
*/
const reverse = convertArrayMethod('reverse')

export { reverse }
2 changes: 1 addition & 1 deletion packages/immutadot/src/array/reverse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { immutaTest } from 'test.utils'
import { reverse } from 'array'

describe('Reverse', () => {
describe('array.reverse', () => {

it('should reverse the elements', () => {
immutaTest((input, path) => {
Expand Down
17 changes: 17 additions & 0 deletions packages/immutadot/src/array/shift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { convertArrayMethod } from './convertArrayMethod'

/**
* Replaces by an array of elements with first element removed.
* @function
* @memberof array
* @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 shift({ nested: { prop: [1, 2, 3, 4] } }, 'nested.prop') // => { nested: { prop: [2, 3, 4] } }
* @see {@link https://mdn.io/Array.prototype.shift|Array.prototype.shift} for more information.
* @since 1.0.0
* @flow
*/
const shift = convertArrayMethod('shift')

export { shift }
42 changes: 42 additions & 0 deletions packages/immutadot/src/array/shift.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-env jest */
import { immutaTest } from 'test.utils'
import { shift } from 'array'

describe('array.shift', () => {

it('should remove first element', () => {
immutaTest((input, path) => {
const output = shift(input, path)
expect(output).toEqual({
nested: { prop: [2, 3, 4] },
other: {},
})
return output
}, {
nested: { prop: [1, 2, 3, 4] },
other: {},
}, 'nested.prop')
})

it('should replace deep undefined with array', () => {
immutaTest((input, path) => {
const output = shift(input, path, () => true)
expect(output).toEqual({ nested: { prop: [] } })
return output
}, undefined, 'nested.prop')
})

it('should wrap value in array and remove it', () => {
immutaTest((input, path) => {
const output = shift(input, path)
expect(output).toEqual({
nested: { prop: [] },
other: {},
})
return output
}, {
nested: { prop: 123 },
other: {},
}, 'nested.prop')
})
})

0 comments on commit c1caf7c

Please sign in to comment.