f-utility
Version:
functional utilities
126 lines (112 loc) • 4.13 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.alterLastIndex = exports.alterFirstIndex = exports.alterIndex = exports.__alterIndex = exports.relativeIndex = exports.__relativeIndex = exports.symmetricDifference = exports.__symmetricDifference = exports.difference = exports.__difference = exports.sort = exports.__sort = exports.concat = exports.join = undefined;
var _entrust = require("entrust");
var _katsuCurry = require("katsu-curry");
var _filter = require("./filter");
/**
* string.prototype.join but curried
* @method join
* @param {string} delimiter
* @param {Array} list
* @returns {string} joined
* @public
* @example
* import {join} from 'f-utility'
* join(`x`, [1,2,3]) // `1x2x3`
*/
var join = exports.join = (0, _entrust.e1)("join");
/**
* return a new array with some other stuff added to it
* @method concat
* @param {Array} - an array
* @param {Array} - another array
* @returns {Array} - combined array
*/
var concat = exports.concat = (0, _katsuCurry.curry)(function (a, b) {
return a.concat(b);
});
/**
* string.prototype.sort but curried
* @method sort
* @param {function} fn
* @param {Array} functor
* @returns {Array} sorted
* @public
* @example
* import {sort} from 'f-utility'
* sort((x) => x % 2, [1,2,3,4,5,6,7,8]) // [ 0, 2, 4, 6, 8, 9, 7, 5, 3, 1 ]
*/
// we can't just invert things with entrust, as sort is a mutating method
var __sort = exports.__sort = function __sort(fn, functor) {
var copy = Array.from(functor);
copy.sort(fn);
return copy;
};
// export const sort = e1(`sort`)
var sort = exports.sort = (0, _katsuCurry.curry)(__sort);
/**
* get the difference between two arrays
* @method difference
* @param {Array} bList - an array
* @param {Array} aList - an array
* @returns {Array} filtered array with differences between the two arrays
* @public
* @example
* import {difference} from 'f-utility'
* difference([1,2,3], [2,4,6]) // [4, 6]
* difference([2,4,6], [1,2,3]) // [1, 3]
*/
var __difference = exports.__difference = function __difference(bList, aList) {
return (0, _filter.filter)(function (x) {
return !aList.includes(x);
}, bList);
};
var difference = exports.difference = (0, _katsuCurry.curry)(__difference);
/**
* get both the differences between two arrays, and if one difference is longer, return it
* @method symmetricDifference
* @param {Array} a - an array
* @param {Array} b - an array
* @returns {Array} filtered array with differences between the two arrays
* @public
* @example
* import {symmetricDifference} from 'f-utility'
* difference([1,2,3], [1,2]) // [3]
*/
var __symmetricDifference = exports.__symmetricDifference = function __symmetricDifference(a, b) {
var ab = difference(a, b);
var ba = difference(b, a);
return ab.concat(ba);
};
var symmetricDifference = exports.symmetricDifference = (0, _katsuCurry.curry)(__symmetricDifference);
/**
* alter the index of a given array input
* @method alterIndex
* @param {number} index - the index to alter
* @param {Function} fn - the function to describe the alteration
* @param {Array} input - the input array
* @returns {Array} an altered copy of the original array
* @public
* @example
* import {alterIndex} from 'f-utility'
* const input = `abcde`.split(``)
* alterIndex(0, () => `butts`, input) // [`butts`, `b`, `c`, `d`, `e`]
* // also works with negative indicies
* alterIndex(-1, () => `x`, input) // [`a`, `b`, `c`, `d`, `x`]
*/
var __relativeIndex = exports.__relativeIndex = function __relativeIndex(length, index) {
return index > -1 ? index : length - Math.abs(index);
};
var relativeIndex = exports.relativeIndex = (0, _katsuCurry.curry)(__relativeIndex);
var __alterIndex = exports.__alterIndex = function __alterIndex(index, fn, input) {
var i = relativeIndex(input.length, index);
var copy = [].concat(input);
copy[i] = fn(copy[i]);
return copy;
};
var alterIndex = exports.alterIndex = (0, _katsuCurry.curry)(__alterIndex);
var alterFirstIndex = exports.alterFirstIndex = alterIndex(0);
var alterLastIndex = exports.alterLastIndex = alterIndex(-1);
;