UNPKG

stick-js

Version:

Fast toolkit for functional programming in JS. Provides idioms for referentially transparent expressions, clear separation of mutable and immutable operations, object factories, function calls based on English grammar, and pipe & compose operators.

51 lines 9.44 kB
"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.toPairs=exports.remapValues=exports.remapTuples=exports.remapKeys=exports.mapValues=exports.mapTuples=exports.mapKeys=exports.fromPairs=exports._remapValuesWithFilter=exports._remapTuplesWithFilter=exports._remapKeysWithFilter=exports._mapValuesWithFilter=exports._mapTuplesWithFilter=exports._mapKeysWithFilter=void 0;var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _internal=require("./internal.js");function _createForOfIteratorHelper(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(!t){if(Array.isArray(r)||(t=_unsupportedIterableToArray(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var _n=0,F=function F(){};return{s:F,n:function n(){return _n>=r.length?{done:!0}:{done:!1,value:r[_n++]};},e:function e(r){throw r;},f:F};}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");}var o,a=!0,u=!1;return{s:function s(){t=t.call(r);},n:function n(){var r=t.next();return a=r.done,r;},e:function e(r){u=!0,o=r;},f:function f(){try{a||null==t.return||t.return();}finally{if(u)throw o;}}};}function _unsupportedIterableToArray(r,a){if(r){if("string"==typeof r)return _arrayLikeToArray(r,a);var t={}.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_arrayLikeToArray(r,a):void 0;}}function _arrayLikeToArray(r,a){(null==a||a>r.length)&&(a=r.length);for(var e=0,n=Array(a);e<a;e++)n[e]=r[e];return n;}/* * These functions are for mapping objects. * * To map arrays, functors or any other data type which has its own `map` method, use our `map` * function, which dispatches to the prototype `map`, but capped. * * An object mapped using one of the `map` functions always results in a new object. The names * `mapKeys`, `mapValues`, and `mapTuples` should make it clear how the mapping happens. * * An object mapped using one of the `remap` functions results in an array. The name `remap` is * meant to make it clear that the shape changes. The `remap` functions follow the same form as the * `map` functions. * * We map only over own, enumerable properties. * * Since arrays are objects in JS, `(re)mapKeys`, `(re)mapValues` and `(re)mapTuples` can also be * used on arrays. The latter is useful when you start with an array and want to end up with an * object. See `fromPairs` for an example of this. * * The WithFilter versions are exported so that map.js can create the mappings, but not further * exported after that. * * Filters work on truthiness, and they take the mapped value as input. * * Map + filter is a convenience for avoiding `reduceObj`. * * It turns out in practice that the 'in' variants, which also loop through non-own properties, are * not that useful for FP in JavaScript, so we don't provide them. * * 1) Inherited properties *tend* to be methods (though not always), so mapping, reducing etc. is * not that useful. * 2) Looping through non-own properties requires `for ... in`, which: * a) doesn't include symbol properties: we consider symbol properties to be useful in FP, in * addition to normal string properties. (`Ramda.keys` doesn't include them either); * b) doesn't produce a guaranteed order. * * Nearly all of the built-in iteration functions in `Object` and `Reflect` only loop through own * properties. * * @todo reduceAs etc. */var canEnum=function canEnum(o,k){return _internal.propertyIsEnumerable.call(o,k);};var remapKeys=exports.remapKeys=function remapKeys(f){return function(o){var ret=[];var _iterator=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step;try{for(_iterator.s();!(_step=_iterator.n()).done;){var k=_step.value;if(canEnum(o,k))ret.push(f(k));}}catch(err){_iterator.e(err);}finally{_iterator.f();}return ret;};};var _remapKeysWithFilter=exports._remapKeysWithFilter=function _remapKeysWithFilter(p){return function(f){return function(o){var ret=[];var _iterator2=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step2;try{for(_iterator2.s();!(_step2=_iterator2.n()).done;){var k=_step2.value;if(!canEnum(o,k))continue;var kk=f(k);if(p(kk))ret.push(f(k));}}catch(err){_iterator2.e(err);}finally{_iterator2.f();}return ret;};};};var remapValues=exports.remapValues=function remapValues(f){return function(o){var ret=[];var _iterator3=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step3;try{for(_iterator3.s();!(_step3=_iterator3.n()).done;){var k=_step3.value;if(canEnum(o,k))ret.push(f(o[k]));}}catch(err){_iterator3.e(err);}finally{_iterator3.f();}return ret;};};var _remapValuesWithFilter=exports._remapValuesWithFilter=function _remapValuesWithFilter(p){return function(f){return function(o){var ret=[];var _iterator4=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step4;try{for(_iterator4.s();!(_step4=_iterator4.n()).done;){var k=_step4.value;if(!canEnum(o,k))continue;var kk=f(o[k]);if(p(kk))ret.push(kk);}}catch(err){_iterator4.e(err);}finally{_iterator4.f();}return ret;};};};var remapTuples=exports.remapTuples=function remapTuples(f){return function(o){var ret=[];var _iterator5=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step5;try{for(_iterator5.s();!(_step5=_iterator5.n()).done;){var k=_step5.value;if(canEnum(o,k))ret.push(f(k,o[k]));}}catch(err){_iterator5.e(err);}finally{_iterator5.f();}return ret;};};var _remapTuplesWithFilter=exports._remapTuplesWithFilter=function _remapTuplesWithFilter(p){return function(f){return function(o){var ret=[];var _iterator6=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step6;try{for(_iterator6.s();!(_step6=_iterator6.n()).done;){var k=_step6.value;if(!canEnum(o,k))continue;var kkvv=f(k,o[k]);var _kkvv=(0,_slicedToArray2.default)(kkvv,2),kk=_kkvv[0],vv=_kkvv[1];if(p(kk,vv))ret.push(kkvv);}}catch(err){_iterator6.e(err);}finally{_iterator6.f();}return ret;};};};// --- note: in all functions which map keys or tuples, it's up to you to ensure that the keys don't // clash. // clashing keys will lead to unpredictable behavior between runtimes. var mapKeys=exports.mapKeys=function mapKeys(f){return function(o){var ret=(0,_internal.safeObject)();var _iterator7=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step7;try{for(_iterator7.s();!(_step7=_iterator7.n()).done;){var k=_step7.value;if(canEnum(o,k))ret[f(k)]=o[k];}}catch(err){_iterator7.e(err);}finally{_iterator7.f();}return ret;};};var _mapKeysWithFilter=exports._mapKeysWithFilter=function _mapKeysWithFilter(p){return function(f){return function(o){var ret=(0,_internal.safeObject)();var _iterator8=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step8;try{for(_iterator8.s();!(_step8=_iterator8.n()).done;){var k=_step8.value;if(!canEnum(o,k))continue;var kk=f(k);if(p(kk))ret[kk]=o[k];}}catch(err){_iterator8.e(err);}finally{_iterator8.f();}return ret;};};};var mapValues=exports.mapValues=function mapValues(f){return function(o){var ret=(0,_internal.safeObject)();var _iterator9=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step9;try{for(_iterator9.s();!(_step9=_iterator9.n()).done;){var k=_step9.value;if(canEnum(o,k))ret[k]=f(o[k]);}}catch(err){_iterator9.e(err);}finally{_iterator9.f();}return ret;};};var _mapValuesWithFilter=exports._mapValuesWithFilter=function _mapValuesWithFilter(p){return function(f){return function(o){var ret=(0,_internal.safeObject)();var _iterator10=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step10;try{for(_iterator10.s();!(_step10=_iterator10.n()).done;){var k=_step10.value;if(!canEnum(o,k))continue;var vv=f(o[k]);if(p(vv))ret[k]=vv;}}catch(err){_iterator10.e(err);}finally{_iterator10.f();}return ret;};};};/* Map an array or an object to an object. * note: filter is truthy, and works on the mapped value. * note: it is up to the caller to ensure that the resulting keys don't clash. * * @experimental You can also use this to map an array to an object, since arrays are also objects. * Be careful, because the index will be a String, not a Number. * @future make separate function for arrays. */var mapTuples=exports.mapTuples=function mapTuples(f){return function(o){var ret=(0,_internal.safeObject)();var _iterator11=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step11;try{for(_iterator11.s();!(_step11=_iterator11.n()).done;){var k=_step11.value;if(!canEnum(o,k))continue;var _f=f(k,o[k]),_f2=(0,_slicedToArray2.default)(_f,2),kk=_f2[0],vv=_f2[1];ret[kk]=vv;}}catch(err){_iterator11.e(err);}finally{_iterator11.f();}return ret;};};var _mapTuplesWithFilter=exports._mapTuplesWithFilter=function _mapTuplesWithFilter(p){return function(f){return function(o){var ret=(0,_internal.safeObject)();var _iterator12=_createForOfIteratorHelper(Reflect.ownKeys(o)),_step12;try{for(_iterator12.s();!(_step12=_iterator12.n()).done;){var k=_step12.value;if(!canEnum(o,k))continue;var _f3=f(k,o[k]),_f4=(0,_slicedToArray2.default)(_f3,2),kk=_f4[0],vv=_f4[1];if(!p(kk,vv))continue;ret[kk]=vv;}}catch(err){_iterator12.e(err);}finally{_iterator12.f();}return ret;};};};var fromPairs=exports.fromPairs=mapTuples(function(_,_ref){var _ref2=(0,_slicedToArray2.default)(_ref,2),k=_ref2[0],v=_ref2[1];return[k,v];});var toPairs=exports.toPairs=remapTuples(function(k,v){return[k,v];});