rambda 10.0.0-beta.1 → 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.
package/dist/rambda.cjs CHANGED
@@ -4,6 +4,35 @@ function addProp(key, value) {
4
4
  return obj => ({ ...obj, [key]: value })
5
5
  }
6
6
 
7
+ function mapFn(
8
+ fn, list
9
+ ){
10
+ let index = 0;
11
+ const willReturn = Array(list.length);
12
+ while (index < list.length) {
13
+ willReturn[index] = fn(list[index], index);
14
+ index++;
15
+ }
16
+ return willReturn
17
+ }
18
+
19
+ function map(fn) {
20
+ return list => mapFn(fn, list)
21
+ }
22
+
23
+ function addPropToObjects (
24
+ property,
25
+ fn
26
+ ){
27
+ return listOfObjects => mapFn(
28
+ (obj) => ({
29
+ ...(obj),
30
+ [property]: fn(obj)
31
+ }),
32
+ listOfObjects
33
+ )
34
+ }
35
+
7
36
  function all(predicate) {
8
37
  return list => {
9
38
  for (let i = 0; i < list.length; i++) {
@@ -203,12 +232,8 @@ function isFalsy(input) {
203
232
  return input === undefined || input === null || Number.isNaN(input) === true
204
233
  }
205
234
 
206
- function defaultTo(defaultArgument, input) {
207
- if (arguments.length === 1) {
208
- return _input => defaultTo(defaultArgument, _input)
209
- }
210
-
211
- return isFalsy(input) ? defaultArgument : input
235
+ function defaultTo(defaultArgument) {
236
+ return input => isFalsy(input) ? defaultArgument : input
212
237
  }
213
238
 
214
239
  function descend(getFunction) {
@@ -220,12 +245,8 @@ function descend(getFunction) {
220
245
  }
221
246
  }
222
247
 
223
- function drop(howManyToDrop, listOrString) {
224
- if (arguments.length === 1) {
225
- return _list => drop(howManyToDrop, _list)
226
- }
227
-
228
- return listOrString.slice(howManyToDrop > 0 ? howManyToDrop : 0)
248
+ function drop(howManyToDrop, ) {
249
+ return list => list.slice(howManyToDrop > 0 ? howManyToDrop : 0)
229
250
  }
230
251
 
231
252
  function dropLast(numberItems) {
@@ -660,6 +681,81 @@ function flatten(list, input) {
660
681
  return willReturn
661
682
  }
662
683
 
684
+ function flattenObjectHelper(obj, accumulator = []){
685
+ const willReturn = {};
686
+ Object.keys(obj).forEach(key => {
687
+ const typeIs = type(obj[ key ]);
688
+ if (typeIs === 'Object'){
689
+ const [ flatResultValue, flatResultPath ] = flattenObjectHelper(obj[ key ],
690
+ [ ...accumulator, key ]);
691
+ willReturn[ flatResultPath.join('.') ] = flatResultValue;
692
+
693
+ return
694
+ } else if (accumulator.length > 0){
695
+ const finalKey = [ ...accumulator, key ].join('.');
696
+ willReturn[ finalKey ] = obj[ key ];
697
+
698
+ return
699
+ }
700
+ willReturn[ key ] = obj[ key ];
701
+ });
702
+ if (accumulator.length > 0) return [ willReturn, accumulator ]
703
+
704
+ return willReturn
705
+ }
706
+
707
+ function transformFlatObject(obj){
708
+ const willReturn = {};
709
+
710
+ const transformFlatObjectFn = objLocal => {
711
+ const willReturnLocal = {};
712
+ Object.keys(objLocal).forEach(key => {
713
+ const typeIs = type(objLocal[ key ]);
714
+ if (typeIs === 'Object'){
715
+ transformFlatObjectFn(objLocal[ key ]);
716
+
717
+ return
718
+ }
719
+ willReturnLocal[ key ] = objLocal[ key ];
720
+ willReturn[ key ] = objLocal[ key ];
721
+ });
722
+
723
+ return willReturnLocal
724
+ };
725
+
726
+ Object.keys(obj).forEach(key => {
727
+ const typeIs = type(obj[ key ]);
728
+ if (typeIs === 'Object'){
729
+ transformFlatObjectFn(obj[ key ]);
730
+
731
+ return
732
+ }
733
+ willReturn[ key ] = obj[ key ];
734
+ });
735
+
736
+ return willReturn
737
+ }
738
+
739
+ function flattenObject(obj){
740
+ const willReturn = {};
741
+
742
+ Object.keys(obj).forEach(key => {
743
+ const typeIs = type(obj[ key ]);
744
+ if (typeIs === 'Object'){
745
+ const flatObject = flattenObjectHelper(obj[ key ]);
746
+ const transformed = transformFlatObject(flatObject);
747
+
748
+ Object.keys(transformed).forEach(keyTransformed => {
749
+ willReturn[ `${ key }.${ keyTransformed }` ] = transformed[ keyTransformed ];
750
+ });
751
+ } else {
752
+ willReturn[ key ] = obj[ key ];
753
+ }
754
+ });
755
+
756
+ return willReturn
757
+ }
758
+
663
759
  function groupByFallback(groupFn, list) {
664
760
  const result = {};
665
761
  for (let i = 0; i < list.length; i++) {
@@ -823,18 +919,6 @@ function lastIndexOf(valueToFind) {
823
919
  return list => _lastIndexOf(valueToFind, list)
824
920
  }
825
921
 
826
- function map(fn) {
827
- return list => {
828
- let index = 0;
829
- const willReturn = Array(list.length);
830
- while (index < list.length) {
831
- willReturn[index] = fn(list[index], index);
832
- index++;
833
- }
834
- return willReturn
835
- }
836
- }
837
-
838
922
  function mapAsync(fn) {
839
923
  return async list => {
840
924
  const willReturn = [];
@@ -899,6 +983,20 @@ function minBy(compareFn, x) {
899
983
  return y => (compareFn(y) < compareFn(x) ? y : x)
900
984
  }
901
985
 
986
+ function modifyItemAtIndex(index, replaceFn) {
987
+ return list => {
988
+ const actualIndex = index < 0 ? list.length + index : index;
989
+ if (index >= list.length || actualIndex < 0) {
990
+ return list
991
+ }
992
+
993
+ const clone = cloneList$1(list);
994
+ clone[actualIndex] = replaceFn(clone[actualIndex]);
995
+
996
+ return clone
997
+ }
998
+ }
999
+
902
1000
  function update(index, newValue) {
903
1001
  return list => {
904
1002
  const clone = cloneList$1(list);
@@ -1026,32 +1124,34 @@ function partitionObject(predicate) {
1026
1124
  }
1027
1125
  }
1028
1126
 
1029
- function path(pathInput, obj) {
1030
- if (arguments.length === 1) {
1031
- return _obj => path(pathInput, _obj)
1032
- }
1033
-
1034
- if (!obj) {
1035
- return undefined
1036
- }
1037
- let willReturn = obj;
1038
- let counter = 0;
1039
-
1040
- const pathArrValue = createPath(pathInput);
1041
-
1042
- while (counter < pathArrValue.length) {
1043
- if (willReturn === null || willReturn === undefined) {
1044
- return undefined
1045
- }
1046
- if (willReturn[pathArrValue[counter]] === null) {
1047
- return undefined
1048
- }
1049
-
1050
- willReturn = willReturn[pathArrValue[counter]];
1051
- counter++;
1052
- }
1127
+ function path(pathInput) {
1128
+ return (obj) => {
1129
+ if (!obj) {
1130
+ return undefined
1131
+ }
1132
+ let willReturn = obj;
1133
+ let counter = 0;
1134
+
1135
+ const pathArrValue = createPath(pathInput);
1136
+
1137
+ while (counter < pathArrValue.length) {
1138
+ if (willReturn === null || willReturn === undefined) {
1139
+ return undefined
1140
+ }
1141
+ if (willReturn[pathArrValue[counter]] === null) {
1142
+ return undefined
1143
+ }
1144
+
1145
+ willReturn = willReturn[pathArrValue[counter]];
1146
+ counter++;
1147
+ }
1148
+
1149
+ return willReturn
1150
+ }
1151
+ }
1053
1152
 
1054
- return willReturn
1153
+ function pathSatisfies(fn, pathInput) {
1154
+ return obj => Boolean(fn(path(pathInput)(obj)))
1055
1155
  }
1056
1156
 
1057
1157
  /**
@@ -1260,7 +1360,7 @@ function propOr(defaultValue, property) {
1260
1360
  return defaultValue
1261
1361
  }
1262
1362
 
1263
- return defaultTo(defaultValue, obj[property])
1363
+ return defaultTo(defaultValue)(obj[property])
1264
1364
  }
1265
1365
  }
1266
1366
 
@@ -1268,42 +1368,33 @@ function propSatisfies(predicate, property) {
1268
1368
  return obj => predicate(obj[property])
1269
1369
  }
1270
1370
 
1271
- function range(start) {
1272
- return end => {
1273
- if (Number.isNaN(Number(start)) || Number.isNaN(Number(end))) {
1274
- throw new TypeError('Both arguments to range must be numbers')
1275
- }
1276
-
1277
- if (end <= start) {
1278
- return []
1279
- }
1371
+ function rangeDescending(start, end) {
1372
+ const len = start - end;
1373
+ const willReturn = Array(len);
1280
1374
 
1281
- const len = end - start;
1282
- const willReturn = Array(len);
1283
-
1284
- for (let i = 0; i < len + 1; i++) {
1285
- willReturn[i] = start + i;
1286
- }
1375
+ for (let i = 0; i < len; i++) {
1376
+ willReturn[i] = start - i;
1377
+ }
1287
1378
 
1288
- return willReturn
1289
- }
1379
+ return willReturn
1290
1380
  }
1291
1381
 
1292
- function rangeDescending(start) {
1382
+ function range(start) {
1293
1383
  return end => {
1294
1384
  if (Number.isNaN(Number(start)) || Number.isNaN(Number(end))) {
1295
1385
  throw new TypeError('Both arguments to range must be numbers')
1296
1386
  }
1297
1387
 
1298
- if (end >= start) {
1388
+ if (end === start) {
1299
1389
  return []
1300
1390
  }
1391
+ if (end < start) return rangeDescending(start,end)
1301
1392
 
1302
- const len = start - end;
1393
+ const len = end - start;
1303
1394
  const willReturn = Array(len);
1304
1395
 
1305
- for (let i = 0; i < len + 1; i++) {
1306
- willReturn[i] = start - i;
1396
+ for (let i = 0; i < len; i++) {
1397
+ willReturn[i] = start + i;
1307
1398
  }
1308
1399
 
1309
1400
  return willReturn
@@ -1314,20 +1405,6 @@ function replace(pattern, replacer) {
1314
1405
  return str => str.replace(pattern, replacer)
1315
1406
  }
1316
1407
 
1317
- function replaceItemAtIndex(index, replaceFn) {
1318
- return list => {
1319
- const actualIndex = index < 0 ? list.length + index : index;
1320
- if (index >= list.length || actualIndex < 0) {
1321
- return list
1322
- }
1323
-
1324
- const clone = cloneList$1(list);
1325
- clone[actualIndex] = replaceFn(clone[actualIndex]);
1326
-
1327
- return clone
1328
- }
1329
- }
1330
-
1331
1408
  function shuffle(listInput) {
1332
1409
  const list = cloneList(listInput);
1333
1410
  let counter = list.length;
@@ -1346,21 +1423,42 @@ function sort(sortFn) {
1346
1423
  return list => cloneList$1(list).sort(sortFn)
1347
1424
  }
1348
1425
 
1426
+ function sortByFn (
1427
+ sortFn,
1428
+ list,
1429
+ descending
1430
+ ){
1431
+ const clone = cloneList$1(list);
1432
+
1433
+ return clone.sort((a, b) => {
1434
+ const aSortResult = sortFn(a);
1435
+ const bSortResult = sortFn(b);
1436
+
1437
+ if (aSortResult === bSortResult) {
1438
+ return 0
1439
+ }
1440
+ if(
1441
+ descending
1442
+ ) return aSortResult > bSortResult ? -1 : 1
1443
+
1444
+ return aSortResult < bSortResult ? -1 : 1
1445
+ })
1446
+ }
1447
+
1349
1448
  function sortBy(sortFn) {
1350
- return list => {
1351
- const clone = cloneList$1(list);
1449
+ return list => sortByFn(sortFn, list, false)
1450
+ }
1352
1451
 
1353
- return clone.sort((a, b) => {
1354
- const aSortResult = sortFn(a);
1355
- const bSortResult = sortFn(b);
1452
+ function sortByDescending(sortFn) {
1453
+ return list => sortByFn(sortFn, list, true)
1454
+ }
1356
1455
 
1357
- if (aSortResult === bSortResult) {
1358
- return 0
1359
- }
1456
+ function sortByPath(sortPath) {
1457
+ return list => sortBy(path(sortPath))(list)
1458
+ }
1360
1459
 
1361
- return aSortResult < bSortResult ? -1 : 1
1362
- })
1363
- }
1460
+ function sortByPathDescending(sortPath) {
1461
+ return list => sortByDescending(path(sortPath))(list)
1364
1462
  }
1365
1463
 
1366
1464
  function sortObject(predicate) {
@@ -1682,6 +1780,7 @@ exports._includes = _includes;
1682
1780
  exports._indexOf = _indexOf;
1683
1781
  exports._lastIndexOf = _lastIndexOf;
1684
1782
  exports.addProp = addProp;
1783
+ exports.addPropToObjects = addPropToObjects;
1685
1784
  exports.all = all;
1686
1785
  exports.allPass = allPass;
1687
1786
  exports.any = any;
@@ -1717,6 +1816,8 @@ exports.findLastIndex = findLastIndex;
1717
1816
  exports.findNth = findNth;
1718
1817
  exports.flatMap = flatMap;
1719
1818
  exports.flatten = flatten;
1819
+ exports.flattenObject = flattenObject;
1820
+ exports.flattenObjectHelper = flattenObjectHelper;
1720
1821
  exports.groupBy = groupBy;
1721
1822
  exports.groupByFallback = groupByFallback;
1722
1823
  exports.head = head;
@@ -1732,6 +1833,7 @@ exports.last = last;
1732
1833
  exports.lastIndexOf = lastIndexOf;
1733
1834
  exports.map = map;
1734
1835
  exports.mapAsync = mapAsync;
1836
+ exports.mapFn = mapFn;
1735
1837
  exports.mapKeys = mapKeys;
1736
1838
  exports.mapObject = mapObject;
1737
1839
  exports.mapObjectAsync = mapObjectAsync;
@@ -1741,6 +1843,7 @@ exports.maxBy = maxBy;
1741
1843
  exports.merge = merge;
1742
1844
  exports.mergeTypes = mergeTypes;
1743
1845
  exports.minBy = minBy;
1846
+ exports.modifyItemAtIndex = modifyItemAtIndex;
1744
1847
  exports.modifyProp = modifyProp;
1745
1848
  exports.none = none;
1746
1849
  exports.objOf = objOf;
@@ -1749,6 +1852,7 @@ exports.omit = omit;
1749
1852
  exports.partition = partition;
1750
1853
  exports.partitionObject = partitionObject;
1751
1854
  exports.path = path;
1855
+ exports.pathSatisfies = pathSatisfies;
1752
1856
  exports.permutations = permutations;
1753
1857
  exports.pick = pick;
1754
1858
  exports.pipe = pipe;
@@ -1760,15 +1864,17 @@ exports.propEq = propEq;
1760
1864
  exports.propOr = propOr;
1761
1865
  exports.propSatisfies = propSatisfies;
1762
1866
  exports.range = range;
1763
- exports.rangeDescending = rangeDescending;
1764
1867
  exports.reduce = reduce;
1765
1868
  exports.reject = reject;
1766
1869
  exports.rejectObject = rejectObject;
1767
1870
  exports.replace = replace;
1768
- exports.replaceItemAtIndex = replaceItemAtIndex;
1769
1871
  exports.shuffle = shuffle;
1770
1872
  exports.sort = sort;
1771
1873
  exports.sortBy = sortBy;
1874
+ exports.sortByDescending = sortByDescending;
1875
+ exports.sortByFn = sortByFn;
1876
+ exports.sortByPath = sortByPath;
1877
+ exports.sortByPathDescending = sortByPathDescending;
1772
1878
  exports.sortObject = sortObject;
1773
1879
  exports.sortWith = sortWith;
1774
1880
  exports.split = split;
@@ -1781,6 +1887,7 @@ exports.takeLastWhile = takeLastWhile;
1781
1887
  exports.takeWhile = takeWhile;
1782
1888
  exports.tap = tap;
1783
1889
  exports.test = test;
1890
+ exports.transformFlatObject = transformFlatObject;
1784
1891
  exports.tryCatch = tryCatch;
1785
1892
  exports.type = type;
1786
1893
  exports.union = union;