Skip to content

Commit

Permalink
Merge pull request #175 from dhershman1/dev
Browse files Browse the repository at this point in the history
v2.1.0
  • Loading branch information
dhershman1 authored Jan 28, 2024
2 parents 7759a4a + 184a5a3 commit 268a9df
Show file tree
Hide file tree
Showing 15 changed files with 1,872 additions and 2,721 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/kyanite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v2
Expand All @@ -37,7 +37,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v2
Expand Down
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Changelog

## v2.1.0

### New

- Added new `takeLast` function
- Which takes from the end of an array instead of the beginning
- Added new `dropLast` function
- Returns a list containing all but the last n elements of the given list
- Added new `pick` function
- Picks only the requested keys from a provided object

### Improved

- Updated `types` for `pathOr` so that it can be specified if needed (still defaults to `any`)

### Fixed

- Updated `drop` to use `Math.max` when deciding a starting index
- This shouldn't affect the output of `drop` which is why its not a breaking change
- It does however allow usage in other functions to work as expected

### Chore

- Updated Dependencies

## v2.0.0

### Breaking Changes
Expand Down
4,278 changes: 1,575 additions & 2,703 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kyanite",
"version": "2.0.1",
"version": "2.1.0",
"description": "A small library of pure functional utilities to make life easier and data better",
"main": "dist/kyanite.min.cjs",
"type": "module",
Expand Down Expand Up @@ -30,20 +30,20 @@
}
},
"devDependencies": {
"@babel/core": "7.22.10",
"@babel/plugin-transform-object-assign": "7.22.5",
"@babel/preset-env": "7.22.10",
"@rollup/plugin-babel": "6.0.3",
"@rollup/plugin-terser": "0.4.3",
"@babel/core": "7.23.5",
"@babel/plugin-transform-object-assign": "7.23.3",
"@babel/preset-env": "7.23.5",
"@rollup/plugin-babel": "6.0.4",
"@rollup/plugin-terser": "0.4.4",
"globby": "13.2.2",
"jsdoc": "4.0.2",
"npm-run-all": "4.1.5",
"pinet": "1.1.4",
"rollup": "3.28.0",
"rollup": "4.7.0",
"rollup-plugin-filesize": "10.0.0",
"standard": "17.1.0",
"tap-on": "0.3.1",
"tape": "5.6.6"
"tap-on": "1.0.0",
"tape": "5.7.2"
},
"scripts": {
"test": "tape tests/**/*.js | tap-on -u",
Expand Down
4 changes: 3 additions & 1 deletion src/array/drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import _curry2 from '../_internals/_curry2.js'
* import { drop } from 'kyanite'
*
* drop(3, [1, 2, 3, 4, 5]) // => [4, 5]
* drop(6, [1, 2, 3, 4, 5]) // => []
* drop(-1, [1, 2, 3, 4, 5]) // => [1, 2, 3, 4, 5]
*
* // It's also curried
*
* const d = drop(3)
*
* d([1, 2, 3, 4, 5]) // => [4, 5]
*/
const drop = (i, list) => list.slice(i, Infinity)
const drop = (i, list) => list.slice(Math.max(0, i), Infinity)

export default _curry2(drop)
28 changes: 28 additions & 0 deletions src/array/dropLast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import _curry2 from '../_internals/_curry2.js'
import take from './take.js'

/**
* @name dropLast
* @since v2.1.0
* @function
* @category Array
* @sig Number -> Array -> Array
* @description Returns a list containing all but the last n elements of the given list.
* @param {Number} n The number of values we want to drop
* @param {Array} list The array we want to drop from
* @return {Array} An array with the indicated values removed from the array
*
* @example
* import { dropLast } from 'kyanite'
*
* dropLast(3, [1, 2, 3, 4, 5]) // => [1, 2]
*
* // It's also curried
*
* const d = dropLast(3)
*
* d([1, 2, 3, 4, 5]) // => [1, 2]
*/
const dropLast = (n, list) => take(n < list.length ? list.length - n : 0, list)

export default _curry2(dropLast)
28 changes: 28 additions & 0 deletions src/array/takeLast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import _curry2 from '../_internals/_curry2.js'
import drop from './drop.js'

/**
* @name takeLast
* @function
* @since v2.1.0
* @category Array
* @sig Number -> Array -> Array
* @description Returns a new list containing the last n elements of the given list. If n > list.length, returns a list of list.length elements.
* @param {Number} n The index we want our take to start at
* @param {Array|String} list The array we are taking from
* @return {Array|String} A new array of the values taken
*
* @example
* import { takeLast } from 'kyanite'
*
* takeLast(3, [1, 2, 3, 4, 5]) // => [3, 4, 5]
*
* // It's also curried
*
* const t = takeLast(3)
*
* t([1, 2, 3, 4, 5]) // => [3, 4, 5]
*/
const takeLast = (n, list) => drop(n >= 0 ? list.length - n : 0, list)

export default _curry2(takeLast)
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { default as concatMap } from './array/concatMap.js'
export { default as countBy } from './array/countBy.js'
export { default as difference } from './array/difference.js'
export { default as drop } from './array/drop.js'
export { default as dropLast } from './array/dropLast.js'
export { default as dropWhile } from './array/dropWhile.js'
export { default as ensureArray } from './array/ensureArray.js'
export { default as every } from './array/every.js'
Expand Down Expand Up @@ -34,6 +35,7 @@ export { default as sort } from './array/sort.js'
export { default as sortBy } from './array/sortBy.js'
export { default as sortWith } from './array/sortWith.js'
export { default as take } from './array/take.js'
export { default as takeLast } from './array/takeLast.js'
export { default as takeWhile } from './array/takeWhile.js'
export { default as union } from './array/union.js'
export { default as uniq } from './array/uniq.js'
Expand Down Expand Up @@ -137,6 +139,7 @@ export { default as over } from './object/over.js'
export { default as path } from './object/path.js'
export { default as pathOr } from './object/pathOr.js'
export { default as pathSatisfies } from './object/pathSatisfies.js'
export { default as pick } from './object/pick.js'
export { default as plan } from './object/plan.js'
export { default as prop } from './object/prop.js'
export { default as propEq } from './object/propEq.js'
Expand Down
41 changes: 41 additions & 0 deletions src/object/pick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import _curry2 from '../_internals/_curry2.js'

/**
* @name pick
* @function
* @since v2.1.0
* @category Object
* @sig [k] -> { k: a } -> { k: a }
* @description Picks only the requested keys from a provided object
* @param {Array} keys The keys we want to pick out of the object
* @param {Object} obj The object to pull the data from
* @return {Object} Returns a new object of only the picked keys provided
*
* @example
* import { pick } from 'kyanite'
*
* pick(['a', 'd'], { a: 1, b: 2, c: 3, d: 4 }) // => { a: 1, d: 4 }
* pick(['a', 'e', 'f'], { a: 3 }) // => { a: 3 }
*
* // Is also curried
*
* const picker = pick(['a', 'd'])
*
* picker({ a: 1, b: 2, c: 3, d: 4 }) // => { a: 1, d: 4 }
*/
const pick = _curry2(function pick (keys, obj) {
const result = {}
let idx = 0

while (idx < keys.length) {
if (keys[idx] in obj) {
result[keys[idx]] = obj[keys[idx]]
}

idx += 1
}

return result
})

export default pick
14 changes: 14 additions & 0 deletions tests/array/drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@ test('drop -- Handles dropping values', t => {
t.end()
})

test('drop -- Handles dropping values when number is larger than length', t => {
const results = drop(6, [1, 2, 3, 4, 5])

t.same(results, [])
t.end()
})

test('drop -- Handles invalid data sets', t => {
const results = drop(3, [])

t.same(results, [])
t.end()
})

test('drop -- Handles negative index', t => {
const results = drop(-1, [1, 2, 3, 4, 5])

t.same(results, [1, 2, 3, 4, 5])
t.end()
})

test('drop -- Is curried', t => {
const d = drop(3)

Expand Down
30 changes: 30 additions & 0 deletions tests/array/dropLast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import dropLast from '../../src/array/dropLast.js'
import test from 'tape'

test('dropLast -- Handles dropping values', t => {
const results = dropLast(3, [1, 2, 3, 4, 5])

t.same(results, [1, 2])
t.end()
})

test('dropLast -- Handles dropping values when number is larger than length', t => {
const results = dropLast(6, [1, 2, 3, 4, 5])

t.same(results, [])
t.end()
})

test('dropLast -- Handles invalid data sets', t => {
const results = dropLast(3, [])

t.same(results, [])
t.end()
})

test('dropLast -- Is curried', t => {
const d = dropLast(3)

t.same(d([1, 2, 3, 4, 5]), [1, 2])
t.end()
})
38 changes: 38 additions & 0 deletions tests/array/takeLast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import takeLast from '../../src/array/takeLast.js'
import test from 'tape'

test('takeLast -- Handles taking values from an array', t => {
const results = takeLast(3, [1, 2, 3, 4, 5])

t.same(results, [3, 4, 5])
t.end()
})

test('takeLast -- Handles values if the number is bigger than the length', t => {
const results = takeLast(7, [1, 2, 3, 4, 5])

t.same(results, [1, 2, 3, 4, 5])
t.end()
})

test('takeLast -- Handles taking values from a string', t => {
const results = takeLast(3, '12345')

t.same(results, '345')
t.end()
})

test('takeLast -- Handles empty arrays passed in', t => {
const results = takeLast(3, [])

t.same(results, [])
t.end()
})

test('takeLast -- Is curried', t => {
const ta = takeLast(3)

t.same(ta([1, 2, 3, 4, 5]), [3, 4, 5], 'curried result returned [3, 4, 5]')
t.same(ta([]), [], 'curried result returned an empty array')
t.end()
})
34 changes: 34 additions & 0 deletions tests/object/pick.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import test from 'tape'
import pick from '../../src/object/pick.js'

test('pick -- picks the requested keys', t => {
const results = pick(['a', 'd'], {
a: 1,
b: 2,
c: 3,
d: 4
})

t.same(results, { a: 1, d: 4 })
t.end()
})

test('pick -- picks only the requested keys', t => {
const results = pick(['a', 'e', 'f'], {
a: 1,
b: 2,
c: 3,
d: 4
})

t.same(results, { a: 1 })
t.end()
})

test('pick -- is curried', t => {
const picker = pick(['a', 'd'])

t.same(picker({ a: 1, b: 2, c: 3, d: 4 }), { a: 1, d: 4 })
t.same(picker({ a: 1, b: 2, c: 3 }), { a: 1 })
t.end()
})
Loading

0 comments on commit 268a9df

Please sign in to comment.