rambda 10.0.0-alpha.0 → 10.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +125 -16
  2. package/README.md +8035 -3592
  3. package/dist/{rambda.esm.js → rambda.cjs} +800 -385
  4. package/dist/rambda.js +673 -486
  5. package/dist/rambda.umd.js +701 -388
  6. package/immutable.d.ts +896 -345
  7. package/index.d.ts +896 -345
  8. package/package.json +22 -15
  9. package/rambda.js +23 -2
  10. package/src/addProp.js +3 -0
  11. package/src/addPropToObjects.js +14 -0
  12. package/src/append.js +5 -5
  13. package/src/ascend.js +16 -0
  14. package/src/compact.js +12 -0
  15. package/src/concat.js +1 -1
  16. package/src/count.js +7 -7
  17. package/src/countBy.js +12 -12
  18. package/src/createObjectFromKeys.js +10 -0
  19. package/src/defaultTo.js +2 -6
  20. package/src/descend.js +10 -0
  21. package/src/drop.js +2 -6
  22. package/src/dropLast.js +1 -3
  23. package/src/eqProps.js +1 -2
  24. package/src/equals.js +2 -2
  25. package/src/evolve.js +2 -23
  26. package/src/filter.js +14 -14
  27. package/src/find.js +10 -10
  28. package/src/findIndex.js +9 -9
  29. package/src/findLast.js +8 -8
  30. package/src/findLastIndex.js +8 -8
  31. package/src/findNth.js +16 -0
  32. package/src/flattenObject.js +76 -0
  33. package/src/groupBy.js +14 -13
  34. package/src/includes.js +12 -13
  35. package/src/interpolate.js +29 -0
  36. package/src/intersection.js +1 -1
  37. package/src/intersperse.js +12 -12
  38. package/src/join.js +1 -1
  39. package/src/map.js +13 -9
  40. package/src/mapAsync.js +3 -3
  41. package/src/mapKeys.js +11 -0
  42. package/src/mapObjectAsync.js +9 -9
  43. package/src/mapParallelAsync.js +3 -0
  44. package/src/match.js +5 -5
  45. package/src/{replaceItemAtIndex.js → modifyItemAtIndex.js} +1 -1
  46. package/src/modifyProp.js +20 -0
  47. package/src/objectIncludes.js +12 -0
  48. package/src/partition.js +13 -37
  49. package/src/partitionObject.js +15 -0
  50. package/src/path.js +24 -26
  51. package/src/pathSatisfies.js +5 -0
  52. package/src/permutations.js +40 -0
  53. package/src/pipeAsync.js +7 -6
  54. package/src/pluck.js +9 -9
  55. package/src/prepend.js +1 -1
  56. package/src/propOr.js +1 -1
  57. package/src/propSatisfies.js +1 -3
  58. package/src/range.js +29 -13
  59. package/src/reject.js +1 -1
  60. package/src/rejectObject.js +13 -0
  61. package/src/shuffle.js +13 -0
  62. package/src/sort.js +1 -1
  63. package/src/sortBy.js +20 -9
  64. package/src/sortByDescending.js +5 -0
  65. package/src/sortByPath.js +6 -0
  66. package/src/sortByPathDescending.js +6 -0
  67. package/src/sortObject.js +15 -0
  68. package/src/sortWith.js +8 -8
  69. package/src/split.js +2 -2
  70. package/src/splitEvery.js +11 -11
  71. package/src/takeLastWhile.js +18 -18
  72. package/src/tap.js +4 -4
  73. package/src/test.js +1 -1
  74. package/src/uniqBy.js +4 -4
  75. package/src/uniqWith.js +10 -10
  76. package/src/modifyPath.js +0 -30
package/src/includes.js CHANGED
@@ -2,18 +2,17 @@ import { isArray } from './_internals/isArray.js'
2
2
  import { _indexOf } from './equals.js'
3
3
 
4
4
  export function includes(valueToFind) {
5
- return iterable =>
6
- {
7
- if (typeof iterable === 'string') {
8
- return iterable.includes(valueToFind)
9
- }
10
- if (!iterable) {
11
- throw new TypeError(`Cannot read property \'indexOf\' of ${iterable}`)
12
- }
13
- if (!isArray(iterable)) {
14
- return false
15
- }
5
+ return iterable => {
6
+ if (typeof iterable === 'string') {
7
+ return iterable.includes(valueToFind)
8
+ }
9
+ if (!iterable) {
10
+ throw new TypeError(`Cannot read property \'indexOf\' of ${iterable}`)
11
+ }
12
+ if (!isArray(iterable)) {
13
+ return false
14
+ }
16
15
 
17
- return _indexOf(valueToFind, iterable) > -1
18
- }
16
+ return _indexOf(valueToFind, iterable) > -1
17
+ }
19
18
  }
@@ -0,0 +1,29 @@
1
+ const getOccurrences = input => input.match(/{{\s*.+?\s*}}/g)
2
+ const getOccurrenceProp = occurrence => occurrence.replace(/{{\s*|\s*}}/g, '')
3
+
4
+ const replace = ({ inputHolder, prop, replacer }) => {
5
+ const regexBase = `{{${prop}}}`
6
+ const regex = new RegExp(regexBase, 'g')
7
+ return inputHolder.replace(regex, replacer)
8
+ }
9
+
10
+ export function interpolate(input) {
11
+ return templateInput => {
12
+ const occurrences = getOccurrences(input)
13
+ if (occurrences === null) {
14
+ return input
15
+ }
16
+ let inputHolder = input
17
+
18
+ for (const occurrence of occurrences) {
19
+ const prop = getOccurrenceProp(occurrence)
20
+ inputHolder = replace({
21
+ inputHolder,
22
+ prop,
23
+ replacer: templateInput[prop],
24
+ })
25
+ }
26
+
27
+ return inputHolder
28
+ }
29
+ }
@@ -2,5 +2,5 @@ import { filter } from './filter.js'
2
2
  import { includes } from './includes.js'
3
3
 
4
4
  export function intersection(listA) {
5
- return listB =>filter(x => includes(x)(listA))(listB)
5
+ return listB => filter(x => includes(x)(listA))(listB)
6
6
  }
@@ -1,17 +1,17 @@
1
1
  export function intersperse(separator) {
2
- return list => {
3
- let index = -1
4
- const len = list.length
5
- const willReturn = []
2
+ return list => {
3
+ let index = -1
4
+ const len = list.length
5
+ const willReturn = []
6
6
 
7
- while (++index < len) {
8
- if (index === len - 1) {
9
- willReturn.push(list[index])
10
- } else {
11
- willReturn.push(list[index], separator)
7
+ while (++index < len) {
8
+ if (index === len - 1) {
9
+ willReturn.push(list[index])
10
+ } else {
11
+ willReturn.push(list[index], separator)
12
+ }
12
13
  }
13
- }
14
14
 
15
- return willReturn
16
- }
15
+ return willReturn
16
+ }
17
17
  }
package/src/join.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export function join(glue) {
2
- return list=> list.join(glue)
2
+ return list => list.join(glue)
3
3
  }
package/src/map.js CHANGED
@@ -1,11 +1,15 @@
1
+ export function mapFn(
2
+ fn, list
3
+ ){
4
+ let index = 0
5
+ const willReturn = Array(list.length)
6
+ while (index < list.length) {
7
+ willReturn[index] = fn(list[index], index)
8
+ index++
9
+ }
10
+ return willReturn
11
+ }
12
+
1
13
  export function map(fn) {
2
- return list => {
3
- let index = 0
4
- const willReturn = Array(list.length)
5
- while (index < list.length) {
6
- willReturn[index] = fn(list[index], index)
7
- index++
8
- }
9
- return willReturn
10
- }
14
+ return list => mapFn(fn, list)
11
15
  }
package/src/mapAsync.js CHANGED
@@ -1,11 +1,11 @@
1
1
  export function mapAsync(fn) {
2
- return async list => {
3
- const willReturn = []
2
+ return async list => {
3
+ const willReturn = []
4
4
  let i = 0
5
5
  for (const a of list) {
6
6
  willReturn.push(await fn(a, i++))
7
7
  }
8
8
 
9
9
  return willReturn
10
- }
10
+ }
11
11
  }
package/src/mapKeys.js ADDED
@@ -0,0 +1,11 @@
1
+ export function mapKeys(fn) {
2
+ return obj => {
3
+ const willReturn = {}
4
+
5
+ Object.keys(obj).forEach(key => {
6
+ willReturn[fn(key, obj[key])] = obj[key]
7
+ })
8
+
9
+ return willReturn
10
+ }
11
+ }
@@ -1,10 +1,10 @@
1
1
  export function mapObjectAsync(fn) {
2
- return async obj => {
3
- const willReturn = {}
4
- for (const prop in obj){
5
- willReturn[ prop ] = await fn(obj[ prop ], prop)
6
- }
7
-
8
- return willReturn
9
- }
10
- }
2
+ return async obj => {
3
+ const willReturn = {}
4
+ for (const prop in obj) {
5
+ willReturn[prop] = await fn(obj[prop], prop)
6
+ }
7
+
8
+ return willReturn
9
+ }
10
+ }
@@ -0,0 +1,3 @@
1
+ export function mapParallelAsync(fn) {
2
+ return async list => Promise.all(list.map((x, i) => fn(x, i)))
3
+ }
package/src/match.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export function match(pattern) {
2
- return input => {
3
- const willReturn = input.match(pattern)
4
-
5
- return willReturn === null ? [] : willReturn
6
- }
2
+ return input => {
3
+ const willReturn = input.match(pattern)
4
+
5
+ return willReturn === null ? [] : willReturn
6
+ }
7
7
  }
@@ -1,6 +1,6 @@
1
1
  import { cloneList } from './_internals/cloneList.js'
2
2
 
3
- export function replaceItemAtIndex(index, replaceFn) {
3
+ export function modifyItemAtIndex(index, replaceFn) {
4
4
  return list => {
5
5
  const actualIndex = index < 0 ? list.length + index : index
6
6
  if (index >= list.length || actualIndex < 0) {
@@ -0,0 +1,20 @@
1
+ import { isArray } from './_internals/isArray.js'
2
+ import { update } from './update.js'
3
+
4
+ function modifyFn(property, fn, list) {
5
+ if (list[property] === undefined) {
6
+ return list
7
+ }
8
+ if (isArray(list)) {
9
+ return update(property, fn(list[property]))(list)
10
+ }
11
+
12
+ return {
13
+ ...list,
14
+ [property]: fn(list[property]),
15
+ }
16
+ }
17
+
18
+ export function modifyProp(property, fn) {
19
+ return obj => modifyFn(property, fn, obj)
20
+ }
@@ -0,0 +1,12 @@
1
+ import { equals } from './equals.js'
2
+ import { filterObject } from './filterObject.js'
3
+
4
+ export function objectIncludes(condition) {
5
+ return obj => {
6
+ const result = filterObject((conditionValue, conditionProp) =>
7
+ equals(conditionValue)(obj[conditionProp]),
8
+ )(condition)
9
+
10
+ return Object.keys(result).length === Object.keys(condition).length
11
+ }
12
+ }
package/src/partition.js CHANGED
@@ -1,41 +1,17 @@
1
- import { isArray } from './_internals/isArray.js'
2
-
3
- export function partitionObject(predicate, iterable) {
4
- const yes = {}
5
- const no = {}
6
- Object.entries(iterable).forEach(([prop, value]) => {
7
- if (predicate(value, prop)) {
8
- yes[prop] = value
9
- } else {
10
- no[prop] = value
11
- }
12
- })
13
-
14
- return [yes, no]
15
- }
16
-
17
- export function partitionArray(predicate, list, indexed = false) {
18
- const yes = []
19
- const no = []
20
- let counter = -1
21
-
22
- while (counter++ < list.length - 1) {
23
- if (indexed ? predicate(list[counter], counter) : predicate(list[counter])) {
24
- yes.push(list[counter])
25
- } else {
26
- no.push(list[counter])
27
- }
28
- }
29
-
30
- return [yes, no]
31
- }
32
-
33
1
  export function partition(predicate) {
34
- return iterable => {
35
- if (!isArray(iterable)) {
36
- return partitionObject(predicate, iterable)
2
+ return list => {
3
+ const yes = []
4
+ const no = []
5
+ let counter = -1
6
+
7
+ while (counter++ < list.length - 1) {
8
+ if (predicate(list[counter], counter)) {
9
+ yes.push(list[counter])
10
+ } else {
11
+ no.push(list[counter])
12
+ }
37
13
  }
38
14
 
39
- return partitionArray(predicate, iterable)
40
- }
15
+ return [yes, no]
16
+ }
41
17
  }
@@ -0,0 +1,15 @@
1
+ export function partitionObject(predicate) {
2
+ return obj => {
3
+ const yes = {}
4
+ const no = {}
5
+ Object.entries(obj).forEach(([prop, value]) => {
6
+ if (predicate(value, prop)) {
7
+ yes[prop] = value
8
+ } else {
9
+ no[prop] = value
10
+ }
11
+ })
12
+
13
+ return [yes, no]
14
+ }
15
+ }
package/src/path.js CHANGED
@@ -1,29 +1,27 @@
1
1
  import { createPath } from './_internals/createPath.js'
2
2
 
3
- export function path(pathInput, obj) {
4
- if (arguments.length === 1) {
5
- return _obj => path(pathInput, _obj)
6
- }
7
-
8
- if (!obj) {
9
- return undefined
10
- }
11
- let willReturn = obj
12
- let counter = 0
13
-
14
- const pathArrValue = createPath(pathInput)
15
-
16
- while (counter < pathArrValue.length) {
17
- if (willReturn === null || willReturn === undefined) {
18
- return undefined
19
- }
20
- if (willReturn[pathArrValue[counter]] === null) {
21
- return undefined
22
- }
23
-
24
- willReturn = willReturn[pathArrValue[counter]]
25
- counter++
26
- }
27
-
28
- return willReturn
3
+ export function path(pathInput) {
4
+ return (obj) => {
5
+ if (!obj) {
6
+ return undefined
7
+ }
8
+ let willReturn = obj
9
+ let counter = 0
10
+
11
+ const pathArrValue = createPath(pathInput)
12
+
13
+ while (counter < pathArrValue.length) {
14
+ if (willReturn === null || willReturn === undefined) {
15
+ return undefined
16
+ }
17
+ if (willReturn[pathArrValue[counter]] === null) {
18
+ return undefined
19
+ }
20
+
21
+ willReturn = willReturn[pathArrValue[counter]]
22
+ counter++
23
+ }
24
+
25
+ return willReturn
26
+ }
29
27
  }
@@ -0,0 +1,5 @@
1
+ import { path } from './path.js'
2
+
3
+ export function pathSatisfies(fn, pathInput) {
4
+ return obj => Boolean(fn(path(pathInput)(obj)))
5
+ }
@@ -0,0 +1,40 @@
1
+ import { cloneList } from './_internals/cloneList.js'
2
+
3
+ /**
4
+ * Source:
5
+ * https://github.com/denoland/std/blob/main/collections/permutations.ts
6
+ */
7
+ export function permutations(inputArray) {
8
+ const result = [];
9
+ const array = cloneList(inputArray);
10
+ const k = array.length;
11
+ if (k === 0) {
12
+ return result;
13
+ }
14
+
15
+ const c = new Array(k).fill(0);
16
+
17
+ result.push([...array]);
18
+
19
+ let i = 1;
20
+
21
+ while (i < k) {
22
+ if (c[i] < i) {
23
+ if (i % 2 === 0) {
24
+ [array[0], array[i]] = [array[i], array[0]]
25
+ } else {
26
+ [array[c[i]], array[i]] = [array[i], array[c[i]]]
27
+ }
28
+
29
+ result.push([...array]);
30
+
31
+ c[i] += 1;
32
+ i = 1;
33
+ } else {
34
+ c[i] = 0;
35
+ i += 1;
36
+ }
37
+ }
38
+
39
+ return result;
40
+ }
package/src/pipeAsync.js CHANGED
@@ -1,10 +1,11 @@
1
1
  import { type } from './type.js'
2
2
 
3
3
  export async function pipeAsync(input, ...fnList) {
4
- let willReturn = input
5
- for (const fn of fnList) {
6
- const initialResult = fn(willReturn)
7
- willReturn = type(initialResult) === 'Promise' ? await initialResult : initialResult
8
- }
9
- return willReturn
4
+ let willReturn = input
5
+ for (const fn of fnList) {
6
+ const initialResult = fn(willReturn)
7
+ willReturn =
8
+ type(initialResult) === 'Promise' ? await initialResult : initialResult
9
+ }
10
+ return willReturn
10
11
  }
package/src/pluck.js CHANGED
@@ -1,13 +1,13 @@
1
1
  export function pluck(property) {
2
- return list => {
3
- const willReturn = []
2
+ return list => {
3
+ const willReturn = []
4
4
 
5
- list.forEach(x => {
6
- if (x[property] !== undefined) {
7
- willReturn.push(x[property])
8
- }
9
- })
5
+ list.forEach(x => {
6
+ if (x[property] !== undefined) {
7
+ willReturn.push(x[property])
8
+ }
9
+ })
10
10
 
11
- return willReturn
12
- }
11
+ return willReturn
12
+ }
13
13
  }
package/src/prepend.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export function prepend(x) {
2
- return list=> [x].concat(list)
2
+ return list => [x].concat(list)
3
3
  }
package/src/propOr.js CHANGED
@@ -6,6 +6,6 @@ export function propOr(defaultValue, property) {
6
6
  return defaultValue
7
7
  }
8
8
 
9
- return defaultTo(defaultValue, obj[property])
9
+ return defaultTo(defaultValue)(obj[property])
10
10
  }
11
11
  }
@@ -1,5 +1,3 @@
1
- import { prop } from './prop.js'
2
-
3
1
  export function propSatisfies(predicate, property) {
4
- return obj => predicate(prop(property, obj))
2
+ return obj => predicate(obj[property])
5
3
  }
package/src/range.js CHANGED
@@ -1,18 +1,34 @@
1
- export function range(start){
2
- return end => {
3
- if (Number.isNaN(Number(start)) || Number.isNaN(Number(end))){
4
- throw new TypeError('Both arguments to range must be numbers')
5
- }
1
+ function rangeDescending(start, end) {
2
+ const len = start - end
3
+ const willReturn = Array(len)
6
4
 
7
- if (end < start) return []
5
+ for (let i = 0; i < len; i++) {
6
+ willReturn[i] = start - i
7
+ }
8
8
 
9
- const len = end - start
10
- const willReturn = Array(len)
9
+ return willReturn
10
+ }
11
11
 
12
- for (let i = 0; i < len; i++){
13
- willReturn[ i ] = start + i
14
- }
12
+ export function range(start) {
13
+ return end => {
14
+ if (Number.isNaN(Number(start)) || Number.isNaN(Number(end))) {
15
+ throw new TypeError('Both arguments to range must be numbers')
16
+ }
15
17
 
16
- return willReturn
17
- }
18
+ if (end === start) {
19
+ return []
20
+ }
21
+ if (end < start) return rangeDescending(start,end)
22
+
23
+ const len = end - start
24
+ const willReturn = Array(len)
25
+
26
+ for (let i = 0; i < len; i++) {
27
+ willReturn[i] = start + i
28
+ }
29
+
30
+ return willReturn
31
+ }
18
32
  }
33
+
34
+
package/src/reject.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { filter } from './filter.js'
2
2
 
3
3
  export function reject(predicate) {
4
- return list => filter(x => !predicate(x), list)
4
+ return list => filter(x => !predicate(x))(list)
5
5
  }
@@ -0,0 +1,13 @@
1
+ export function rejectObject(predicate) {
2
+ return obj => {
3
+ const willReturn = {}
4
+
5
+ for (const prop in obj) {
6
+ if (!predicate(obj[prop], prop, obj)) {
7
+ willReturn[prop] = obj[prop]
8
+ }
9
+ }
10
+
11
+ return willReturn
12
+ }
13
+ }
package/src/shuffle.js ADDED
@@ -0,0 +1,13 @@
1
+ export function shuffle(listInput) {
2
+ const list = cloneList(listInput)
3
+ let counter = list.length
4
+ while (counter > 0) {
5
+ const index = Math.floor(Math.random() * counter)
6
+ counter--
7
+ const temp = list[counter]
8
+ list[counter] = list[index]
9
+ list[index] = temp
10
+ }
11
+
12
+ return list
13
+ }
package/src/sort.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { cloneList } from './_internals/cloneList.js'
2
2
 
3
- export function sort(sortFn){
3
+ export function sort(sortFn) {
4
4
  return list => cloneList(list).sort(sortFn)
5
5
  }
package/src/sortBy.js CHANGED
@@ -1,16 +1,27 @@
1
1
  import { cloneList } from './_internals/cloneList.js'
2
2
 
3
- export function sortBy(sortFn){
4
- return list => {
5
- const clone = cloneList(list)
3
+ export function sortByFn (
4
+ sortFn,
5
+ list,
6
+ descending
7
+ ){
8
+ const clone = cloneList(list)
6
9
 
7
- return clone.sort((a, b) => {
8
- const aSortResult = sortFn(a)
9
- const bSortResult = sortFn(b)
10
+ return clone.sort((a, b) => {
11
+ const aSortResult = sortFn(a)
12
+ const bSortResult = sortFn(b)
10
13
 
11
- if (aSortResult === bSortResult) return 0
14
+ if (aSortResult === bSortResult) {
15
+ return 0
16
+ }
17
+ if(
18
+ descending
19
+ ) return aSortResult > bSortResult ? -1 : 1
12
20
 
13
- return aSortResult < bSortResult ? -1 : 1
14
- })
21
+ return aSortResult < bSortResult ? -1 : 1
22
+ })
15
23
  }
24
+
25
+ export function sortBy(sortFn) {
26
+ return list => sortByFn(sortFn, list, false)
16
27
  }
@@ -0,0 +1,5 @@
1
+ import { sortByFn } from "./sortBy.js";
2
+
3
+ export function sortByDescending(sortFn) {
4
+ return list => sortByFn(sortFn, list, true)
5
+ }
@@ -0,0 +1,6 @@
1
+ import { path } from './path.js'
2
+ import { sortBy } from './sortBy.js'
3
+
4
+ export function sortByPath(sortPath) {
5
+ return list => sortBy(path(sortPath))(list)
6
+ }
@@ -0,0 +1,6 @@
1
+ import { path } from './path.js'
2
+ import { sortByDescending } from './sortByDescending.js'
3
+
4
+ export function sortByPathDescending(sortPath) {
5
+ return list => sortByDescending(path(sortPath))(list)
6
+ }
@@ -0,0 +1,15 @@
1
+ import { sort } from './sort.js'
2
+
3
+ export function sortObject(predicate) {
4
+ return obj => {
5
+ const keys = Object.keys(obj)
6
+ const sortedKeys = sort((a, b) => predicate(a, b, obj[a], obj[b]))(keys)
7
+
8
+ const toReturn = {}
9
+ sortedKeys.forEach(singleKey => {
10
+ toReturn[singleKey] = obj[singleKey]
11
+ })
12
+
13
+ return toReturn
14
+ }
15
+ }