Comparing version 0.0.1 to 1.0.0
@@ -1,2 +0,2 @@ | ||
!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define("iz",[],r):"object"==typeof exports?exports.iz=r():t.iz=r()}(window,function(){return function(t){var r={};function e(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var o in t)e.d(n,o,function(r){return t[r]}.bind(null,o));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=2)}([function(t,r){},function(t,r,e){"use strict";e.d(r,"a",function(){return u});var n=function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,o,i=e.call(t),u=[];try{for(;(void 0===r||r-- >0)&&!(n=i.next()).done;)u.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return u},o=function(){for(var t=[],r=0;r<arguments.length;r++)t=t.concat(n(arguments[r]));return t},i=function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},u=function(){function t(t,r){this.result=null,this.source=t,r&&(this.equality=r.equality,this.comparator=r.comparator)}return t.prototype.clone=function(){return new t(this.source,this)},t.prototype.act=function(t){return this.action=0,this.replaceWith=null,this.callback(t,this),this.action},t.prototype.stop=function(t){return this.result=t,this.action=1,this},t.prototype.isStopped=function(){return 1===this.action},t.prototype.replace=function(t){return this.replaceWith=t,this.action=3,this},t.prototype.remove=function(){return this.action=2,this},t.prototype.withEquality=function(t){return this.equality=t,this},t.prototype.withComparator=function(t){return this.comparator=t,this.equality||(this.equality=function(r,e){return 0===t(r,e)}),this},t.prototype.desc=function(t){var r=t||this.comparator;return this.comparator=function(t,e){return r(e,t)},this},t.prototype.getEquality=function(r){return r||this.equality||t.EQUALS_STRICT},t.prototype.getComparator=function(t){var r=t||this.comparator;if(!r)throw new Error("A comparator is required for the requested action");return r},t.prototype.empty=function(){return!this.iterate(function(t,r){return r.stop()}).isStopped()},t.prototype.has=function(){return this.iterate(function(t,r){return r.stop()}).isStopped()},t.prototype.contains=function(t,r){var e=this;return this.where(function(n){return e.getEquality(r)(t,n)}).has()},t.prototype.count=function(){var t=0;return this.iterate(function(r,e){return t++}),t},t.prototype.first=function(){return this.iterate(function(t,r){return r.stop(t)}).result},t.prototype.last=function(){var t=null;return this.iterate(function(r,e){return t=r}),t},t.prototype.list=function(t){return void 0===t&&(t=[]),this.iterate(function(r){return t.push(r)}),t},t.prototype.object=function(t,r){return void 0===r&&(r=Object.create(null)),this.iterate(function(e){return r[t(e)]=e}),r},t.prototype.reduce=function(t,r){var e=t;return this.iterate(function(t){return e=r(t,e)}),e},t.prototype.min=function(t){var r=this.getComparator(t);return this.reduce(null,function(t,e){return null===e||r(t,e)<0?t:e})},t.prototype.max=function(t){var r=this.getComparator(t);return this.reduce(null,function(t,e){return null===e||r(t,e)>0?t:e})},t.prototype.erase=function(){return this.iterate(function(t,r){return r.remove()})},t.prototype.extract=function(){var r=[];return this.iterate(function(t,e){return r.push(t)&&e.remove()}),t.array(r)},t.prototype.overwrite=function(t){return this.iterate(function(r,e){return e.replace(t)})},t.prototype.view=function(r,e,n,o){var i=this;return new t(function(t){var u=r();i.iterate(function(r,i){if(e(u,r)){switch(t.act(r)){case 1:i.stop();break;case 2:i.remove();break;case 3:i.replace(t.replaceWith)}n&&n(u,r,i)}else o&&o(u,r,i)})},this)},t.prototype.take=function(r){return r<=0?t.empty():this.view(function(){return{amount:r}},function(t){return t.amount>0},function(t){return t.amount--},function(t,r,e){return e.stop()})},t.prototype.skip=function(t){return this.view(function(){return{skipped:0}},function(r){return r.skipped>=t},function(t){return t.skipped++},function(t){return t.skipped++})},t.prototype.drop=function(t){return this.reverse().skip(t).reverse()},t.prototype.append=function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return t.join.apply(t,o([this],r))},t.prototype.prepend=function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return t.join.apply(t,o(r,[this]))},t.prototype.where=function(t){return this.view(function(){return null},function(r,e){return t(e)})},t.prototype.gt=function(t,r){var e=this;return this.view(function(){return e.getComparator(r)},function(r,e){return r(e,t)>0})},t.prototype.gte=function(t,r){var e=this;return this.view(function(){return e.getComparator(r)},function(r,e){return r(e,t)>=0})},t.prototype.lt=function(t,r){var e=this;return this.view(function(){return e.getComparator(r)},function(r,e){return r(e,t)<0})},t.prototype.lte=function(t,r){var e=this;return this.view(function(){return e.getComparator(r)},function(r,e){return r(e,t)<=0})},t.prototype.exclude=function(t,r){var e=this;return this.view(function(){return e.getEquality(r)},function(r,e){return!t.contains(e,r)})},t.prototype.intersect=function(t,r){var e=this;return this.view(function(){return e.getEquality(r)},function(r,e){return t.contains(e,r)})},t.prototype.unique=function(t){var r=this;return this.view(function(){return{existing:[],isEqual:r.getEquality(t)}},function(t,r){var e=t.existing,n=t.isEqual;return-1===e.findIndex(function(t){return n(t,r)})},function(t,r){return t.existing.push(r)})},t.prototype.duplicates=function(t,r){var e=this;return void 0===t&&(t=!1),this.view(function(){return{existing:[],once:[],isEqual:e.getEquality(r)}},function(r,e){var n=r.existing,o=r.once,i=r.isEqual,u=n.findIndex(function(t){return i(t,e)}),c=-1!==u;return c?(o[u]&&t&&(c=!1),o[u]=!0):n.push(e),c})},t.prototype.viewResolved=function(r){var e=this;return new t(function(t){var n=e.list(),o=[],i=[],u=[],c=!1;if(r(n,function(r,e){var n=t.act(r);return 3!==n&&2!==n||(c=!0,u[e]=r,o[e]=n,i[e]=t.replaceWith),n}),c){var a=0;e.iterate(function(t,r){switch(o[a]){case 2:t===u[a]&&r.remove();break;case 3:t===u[a]&&r.replace(i[a])}a++})}},this)},t.prototype.sorted=function(t){var r=this;return this.viewResolved(function(e,n){var o,u,c=r.getComparator(t),a=e.map(function(t,r){return{item:t,index:r}});a.sort(function(t,r){return c(t.item,r.item)});try{for(var f=i(a),s=f.next();!s.done;s=f.next()){var p=s.value;if(1===n(p.item,p.index))return}}catch(t){o={error:t}}finally{try{s&&!s.done&&(u=f.return)&&u.call(f)}finally{if(o)throw o.error}}})},t.prototype.reverse=function(){return this.viewResolved(function(t,r){for(var e=t.length-1;e>=0;e--)if(1===r(t[e],e))return})},t.prototype.map=function(r,e){var n=this;return void 0===e&&(e=null),new t(function(t){n.iterate(function(n,o){var i=r(n,o);if(void 0!==i)switch(t.act(i)){case 1:o.stop();break;case 2:o.remove();break;case 3:e&&o.replace(e(t.replaceWith,i,n))}})})},t.prototype.iterate=function(t){return this.result=void 0,this.callback=t,this.action=0,this.source(this),this.callback=null,this},t.prototype.withResult=function(t){return this.result&&t(this.result),this},t.prototype[Symbol.iterator]=function(){return this.list().values()},t.array=function(r){return new t(function(t){for(var e=0;e<r.length;e++)switch(t.act(r[e])){case 1:return;case 2:r.splice(e,1),e--;break;case 3:r.splice(e,1,t.replaceWith)}})},t.object=function(r,e){return void 0===e&&(e=!0),new t(function(t){for(var n in r)if(!e||r.hasOwnProperty(n))switch(t.act(r[n])){case 1:return;case 2:delete r[n];break;case 3:r[n]=t.replaceWith}})},t.tree=function(r,e,n){var o=t.empty(),i=function(r){var n=e(r);return n?Array.isArray(n)?t.array(n):n:o},u=function(t,e,o,i,u){switch(e.act(r(t))){case 1:return u&&u.stop(),!1;case 3:if(n)n(t,e.replaceWith);else if(o)throw new Error("replaceValue is required when replacing a value in a tree");break;case 2:if(u)u.remove();else if(o&&i)throw new Error("remove is not supported for breadth-first iteration")}return!0},c=function(t,r,e,n){return!!u(t,r,e,!1,n)&&!i(t).iterate(function(t,n){return c(t,r,e,n)}).isStopped()};return function(r,e,n){return void 0===e&&(e=!0),void 0===n&&(n=!0),new t(function(t){return e?c(r,t,n):function(t,r,e){var n=[];for(n.push(t);n.length>0;){var o=n.shift();if(!u(o,r,e,!0))break;i(o).list(n)}}(r,t,n)})}},t.join=function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return new t(function(t){var e,n;try{for(var o=i(r),u=o.next();!u.done;u=o.next()){var c=u.value;if(c.iterate(function(r,e){switch(t.act(r)){case 2:e.remove();break;case 1:e.stop();break;case 3:e.replace(t.replaceWith)}}),1===c.action)return}}catch(t){e={error:t}}finally{try{u&&!u.done&&(n=o.return)&&n.call(o)}finally{if(e)throw e.error}}})},t.empty=function(){return new t(function(t){})},t.EQUALS_STRICT=function(t,r){return t===r},t.EQUALS_LOOSE=function(t,r){return t==r},t.EQUALS_STRING_SENSITIVE=function(t,r){return 0===t.localeCompare(r)},t.EQUALS_STRING_INSENSITIVE=function(t,r){return 0===t.toLowerCase().localeCompare(r.toLowerCase())},t.COMPARE_NUMBER=function(t,r){return t-r},t.COMPARE_STRING=function(t,r){return t.localeCompare(r)},t.COMPARE_DATE=function(t,r){return(t?t.getTime():0)-(r?r.getTime():0)},t}()},function(t,r,e){"use strict";e.r(r);var n=e(0);for(var o in n)"default"!==o&&function(t){e.d(r,t,function(){return n[t]})}(o);var i=e(1);e.d(r,"Iterate",function(){return i.a})}])}); | ||
!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define("iteratez",[],r):"object"==typeof exports?exports.iteratez=r():t.iteratez=r()}(this,function(){return function(t){var r={};function e(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{enumerable:!0,get:n})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,r){if(1&r&&(t=e(t)),8&r)return t;if(4&r&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(e.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&r&&"string"!=typeof t)for(var o in t)e.d(n,o,function(r){return t[r]}.bind(null,o));return n},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,r){return Object.prototype.hasOwnProperty.call(t,r)},e.p="",e(e.s=3)}([function(t,r,e){"use strict";e.d(r,"a",function(){return i}),e.d(r,"h",function(){return u}),e.d(r,"b",function(){return a}),e.d(r,"c",function(){return c}),e.d(r,"f",function(){return f}),e.d(r,"g",function(){return s}),e.d(r,"d",function(){return p}),e.d(r,"e",function(){return l});var n=e(1),o=function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},i=[function(t){return t instanceof n.a&&t},function(t){return!!Array.isArray(t)&&n.a.array(t)},function(t){return t instanceof Set&&n.a.set(t)},function(t){return t instanceof Map&&n.a.map(t)},function(t){return!(!t||!t[Symbol.iterator])&&n.a.iterable(t)},function(t){return!(!t||!t.entries)&&n.a.hasEntries(t)},function(t){return null==t&&n.a.empty()},function(t){return"object"==typeof t&&n.a.object(t)},function(t){return n.a.array([t])}];function u(t){var r,e;try{for(var n=o(i),u=n.next();!u.done;u=n.next()){var a=(0,u.value)(t);if(!1!==a)return a}}catch(t){r={error:t}}finally{try{u&&!u.done&&(e=n.return)&&e.call(n)}finally{if(r)throw r.error}}}function a(t,r,e,n,o,i){var u=!o(e),a=!o(n);return u!==a?(r?u:a)?-1:1:u?0:t?i(e,n):i(n,e)}function c(t,r,e,n){var o=!e(t);return o===!e(r)&&(!!o||n(t,r))}function f(t,r){void 0===t&&(t=!0),void 0===r&&(r=!1);var e=function(t){return"number"==typeof t&&isFinite(t)},n=function(t,r){return t-r};return function(o,i){return a(t,r,o,i,e,n)}}function s(t,r,e){void 0===t&&(t=!0),void 0===r&&(r=!0),void 0===e&&(e=!1);var n=function(t){return"string"==typeof t},o=t?function(t,r){return t.localeCompare(r)}:function(t,r){return t.toLowerCase().localeCompare(r.toLowerCase())};return function(t,i){return a(r,e,t,i,n,o)}}function p(t,r){void 0===t&&(t=!0),void 0===r&&(r=!1);var e=function(t){return t instanceof Date},n=function(t,r){return t.getTime()-r.getTime()};return function(o,i){return a(t,r,o,i,e,n)}}function l(t,r){void 0===t&&(t=1),void 0===r&&(r=!0);var e=function(t){return t instanceof Date},n=r?function(t){return t.getTime()}:function(t){return t.getTime()+6e4*t.getTimezoneOffset()},o=function(r,e){return n(r)%t==n(e)%t};return function(t,r){return c(t,r,e,o)}}},function(t,r,e){"use strict";e.d(r,"a",function(){return a});var n=e(0),o=function(t,r){var e="function"==typeof Symbol&&t[Symbol.iterator];if(!e)return t;var n,o,i=e.call(t),u=[];try{for(;(void 0===r||r-- >0)&&!(n=i.next()).done;)u.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(e=i.return)&&e.call(i)}finally{if(o)throw o.error}}return u},i=function(){for(var t=[],r=0;r<arguments.length;r++)t=t.concat(o(arguments[r]));return t},u=function(t){var r="function"==typeof Symbol&&t[Symbol.iterator],e=0;return r?r.call(t):{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}}},a=function(){function t(t,r){this.result=null,this.source=t,r&&(this.equality=r.equality,this.comparator=r.comparator)}return t.prototype.clone=function(){return new t(this.source,this)},t.prototype.act=function(t,r){return this.action=0,this.replaceWith=null,this.callback(t,r,this),this.action},t.prototype.stop=function(t){return this.result=t,this.action=1,this},t.prototype.isStopped=function(){return 1===this.action},t.prototype.replace=function(t){return this.replaceWith=t,this.action=3,this},t.prototype.remove=function(){return this.action=2,this},t.prototype.withEquality=function(t){return this.equality=t,this},t.prototype.withComparator=function(t){return this.comparator=t,this.equality||(this.equality=function(r,e){return 0===t(r,e)}),this},t.prototype.withLogic=function(t,r){return this.comparator=t,this.equality=r||function(r,e,n,o){return 0===t(r,e,n,o)},this},t.prototype.numbers=function(t,r){return void 0===t&&(t=!0),void 0===r&&(r=!1),this.withLogic(Object(n.f)(t,r))},t.prototype.strings=function(t,r,e){return void 0===t&&(t=!0),void 0===r&&(r=!0),void 0===e&&(e=!1),this.withLogic(Object(n.g)(t,r,e))},t.prototype.dates=function(t,r,e,o){return void 0===t&&(t=1),void 0===r&&(r=!0),void 0===e&&(e=!0),void 0===o&&(o=!1),this.withLogic(Object(n.d)(e,o),Object(n.e)(t,r))},t.prototype.desc=function(t){var r=t||this.comparator;return this.comparator=function(t,e,n,o){return r(e,t,o,n)},this},t.prototype.getEquality=function(r){return r||this.equality||t.EQUALS_STRICT},t.prototype.getComparator=function(t){var r=t||this.comparator;if(!r)throw new Error("A comparator is required for the requested action");return r},t.prototype.empty=function(){return!this.iterate(function(t,r,e){return e.stop()}).isStopped()},t.prototype.has=function(){return this.iterate(function(t,r,e){return e.stop()}).isStopped()},t.prototype.contains=function(t,r){var e=this;return this.where(function(n,o){return e.getEquality(r)(t,n,void 0,o)}).has()},t.prototype.count=function(){var t=0;return this.iterate(function(){return t++}),t},t.prototype.first=function(){return this.iterate(function(t,r,e){return e.stop(t)}).result},t.prototype.last=function(){var t=null;return this.iterate(function(r){return t=r}),t},t.prototype.array=function(t){return void 0===t&&(t=[]),this.iterate(function(r){return t.push(r)}),t},t.prototype.entries=function(t){return void 0===t&&(t=[]),this.iterate(function(r,e){return t.push([e,r])}),t},t.prototype.object=function(t,r){return void 0===r&&(r=Object.create(null)),this.iterate(function(e){return r[t(e)]=e}),r},t.prototype.set=function(t){return void 0===t&&(t=new Set),this.iterate(function(r){return t.add(r)}),t},t.prototype.map=function(t){return void 0===t&&(t=new Map),this.iterate(function(r,e){return t.set(e,r)}),t},t.prototype.group=function(t,r){return void 0===r&&(r=Object.create(null)),this.iterate(function(e){var n=t(e);n in r?r[n].push(e):r[n]=[e]}),r},t.prototype.reduce=function(t,r){var e=t;return this.iterate(function(t){return e=r(t,e)}),e},t.prototype.min=function(t){var r=this.getComparator(t);return this.reduce(null,function(t,e){return null===e||r(t,e)<0?t:e})},t.prototype.max=function(t){var r=this.getComparator(t);return this.reduce(null,function(t,e){return null===e||r(t,e)>0?t:e})},t.prototype.delete=function(){return this.iterate(function(t,r,e){return e.remove()})},t.prototype.extract=function(){var r=[];return this.iterate(function(t,e,n){return r.push([e,t])&&n.remove()}),t.entries(r)},t.prototype.overwrite=function(t){return this.iterate(function(r,e,n){return n.replace(t)})},t.prototype.fork=function(t){return t(this),this},t.prototype.split=function(t,r){var e=this.where(t),n=this.not(t);return r?(r(e,n),this):{pass:e,fail:n}},t.prototype.unzip=function(t){var r=this.keys(),e=this.values();return t?(t(r,e),this):{keys:r,values:e}},t.prototype.keys=function(){var r=this;return new t(function(t){var e=0;r.iterate(function(r,n,o){switch(t.act(n,e++)){case 1:o.stop();break;case 2:o.remove()}})})},t.prototype.values=function(){var r=this;return new t(function(t){r.iterate(function(r,e,n){var o=0;switch(t.act(r,o++)){case 1:n.stop();break;case 2:n.remove();break;case 3:n.replace(t.replaceWith)}})},this)},t.prototype.view=function(r,e,n,o){var i=this;return new t(function(t){var u=r();i.iterate(function(r,i,a){if(e(u,r,i)){switch(t.act(r,i)){case 1:a.stop();break;case 2:a.remove();break;case 3:a.replace(t.replaceWith)}n&&n(u,r,i,a)}else o&&o(u,r,i,a)})},this)},t.prototype.take=function(r){return r<=0?t.empty():this.view(function(){return{amount:r}},function(t){return t.amount>0},function(t){return t.amount--},function(t,r,e,n){return n.stop()})},t.prototype.skip=function(t){return this.view(function(){return{skipped:0}},function(r){return r.skipped>=t},function(t){return t.skipped++},function(t){return t.skipped++})},t.prototype.drop=function(t){return this.reverse().skip(t).reverse()},t.prototype.append=function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return t.join.apply(t,i([this],r))},t.prototype.prepend=function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return t.join.apply(t,i(r,[this]))},t.prototype.where=function(t){return this.view(function(){return null},function(r,e,n){return t(e,n)})},t.prototype.not=function(t){return this.view(function(){return null},function(r,e,n){return!t(e,n)})},t.prototype.gt=function(t,r){var e=this;return this.view(function(){return e.getComparator(r)},function(r,e,n){return r(e,t,n)>0})},t.prototype.gte=function(t,r){var e=this;return this.view(function(){return e.getComparator(r)},function(r,e,n){return r(e,t,n)>=0})},t.prototype.lt=function(t,r){var e=this;return this.view(function(){return e.getComparator(r)},function(r,e,n){return r(e,t,n)<0})},t.prototype.lte=function(t,r){var e=this;return this.view(function(){return e.getComparator(r)},function(r,e,n){return r(e,t,n)<=0})},t.prototype.exclude=function(t,r){var e=this;return this.view(function(){return[e.getEquality(r),Object(n.h)(t)]},function(t,r){var e=o(t,2),n=e[0];return!e[1].contains(r,n)})},t.prototype.intersect=function(t,r){var e=this;return this.view(function(){return[e.getEquality(r),Object(n.h)(t)]},function(t,r){var e=o(t,2),n=e[0];return e[1].contains(r,n)})},t.prototype.unique=function(t){var r=this;return this.view(function(){return{existing:[],isEqual:r.getEquality(t)}},function(t,r,e){var n=t.existing,i=t.isEqual;return-1===n.findIndex(function(t){var n=o(t,2),u=n[0],a=n[1];return i(a,r,u,e)})},function(t,r,e){return t.existing.push([e,r])})},t.prototype.duplicates=function(t,r){var e=this;return void 0===t&&(t=!1),this.view(function(){return{existing:[],once:[],isEqual:e.getEquality(r)}},function(r,e,n){var i=r.existing,u=r.once,a=r.isEqual,c=i.findIndex(function(t){var r=o(t,2),i=r[0],u=r[1];return a(u,e,i,n)}),f=-1!==c;return f?(u[c]&&t&&(f=!1),u[c]=!0):i.push([n,e]),f})},t.prototype.readonly=function(){var r=this;return new t(function(t){r.iterate(function(r,e,n){1===t.act(r,e)&&n.stop()})},this)},t.prototype.copy=function(){return t.entries(this.entries())},t.prototype.viewResolved=function(r){var e=this;return new t(function(t){var n=e.entries(),o=[],i=[],u=[],a=!1;if(r(n,function(r,e,n){var c=t.act(r,e);return 3!==c&&2!==c||(a=!0,u[n]=r,o[n]=c,i[n]=t.replaceWith),c}),a){var c=0;e.iterate(function(t,r,e){switch(o[c]){case 2:t===u[c]&&e.remove();break;case 3:t===u[c]&&e.replace(i[c])}c++})}},this)},t.prototype.sorted=function(t){var r=this;return this.viewResolved(function(e,n){var i,a,c=r.getComparator(t),f=e.map(function(t,r){var e=o(t,2);return{key:e[0],value:e[1],index:r}});f.sort(function(t,r){return c(t.value,r.value)});try{for(var s=u(f),p=s.next();!p.done;p=s.next()){var l=p.value,h=l.key;if(1===n(l.value,h,l.index))return}}catch(t){i={error:t}}finally{try{p&&!p.done&&(a=s.return)&&a.call(s)}finally{if(i)throw i.error}}})},t.prototype.shuffle=function(t){void 0===t&&(t=1);var r=function(t,r,e){var n=t[r];t[r]=t[e],t[e]=n};return this.viewResolved(function(e,n){for(var i=[],u=e.length,a=0;a<u;a++)i.push(a);for(var c=0;c<t;c++)for(var f=0;f<u;f++){var s=Math.floor(Math.random()*u);r(e,s,f),r(i,s,f)}for(a=0;a<u;a++){var p=o(e[a],2),l=p[0];if(1===n(p[1],l,i[a]))return}})},t.prototype.reverse=function(){return this.viewResolved(function(t,r){for(var e=t.length-1;e>=0;e--){var n=o(t[e],2),i=n[0];if(1===r(n[1],i,e))return}})},t.prototype.transform=function(r,e){var n=this;return void 0===e&&(e=null),new t(function(t){n.iterate(function(n,o,i){var u=r(n,o,i);if(void 0!==u)switch(t.act(u,o)){case 1:i.stop();break;case 2:i.remove();break;case 3:e&&i.replace(e(t.replaceWith,u,n,o))}})})},t.prototype.iterate=function(t){return this.result=void 0,this.callback=t,this.action=0,this.source(this),this.callback=null,this},t.prototype.withResult=function(t){return this.result&&t(this.result),this},t.prototype[Symbol.iterator]=function(){return this.array().values()},t.entries=function(r){return new t(function(t){for(var e=0;e<r.length;e++){var n=o(r[e],2),i=n[0],u=n[1];switch(t.act(u,i)){case 1:return;case 2:r.splice(e,1),e--;break;case 3:r.splice(e,1,[i,t.replaceWith])}}})},t.array=function(r){return new t(function(t){for(var e=0;e<r.length;e++)switch(t.act(r[e],e)){case 1:return;case 2:r.splice(e,1),e--;break;case 3:r.splice(e,1,t.replaceWith)}})},t.zip=function(r,e){var o=Object(n.h)(r),i=Object(n.h)(e);return new t(function(t){var r=o.array(),e=[],n=0;if(i.iterate(function(o,i,u){if(n>=r.length)u.stop();else switch(t.act(o,r[n])){case 1:return;case 2:u.remove(),e.push(n);break;case 3:u.replace(t.replaceWith)}n++}),e.length>0){var u=0;o.iterate(function(t,r,n){u===e[0]?(n.remove(),e.shift()):0===e.length&&n.stop(),u++})}})},t.hasEntries=function(r,e,n){return new t(function(t){for(var i=r.entries(),u=i.next();!u.done;u=i.next()){var a=o(u.value,2),c=a[0],f=a[1];switch(t.act(f,c)){case 1:return;case 2:e&&e(r,c,f);break;case 3:n&&n(r,c,f,t.replaceWith)}}})},t.map=function(r){return t.hasEntries(r,function(t,r){return t.delete(r)},function(t,r,e,n){return t.set(r,n)})},t.set=function(r){return t.hasEntries(r,function(t,r){return t.delete(r)},function(t,e,n,o){return r.delete(n)&&r.add(o)})},t.iterable=function(r){return new t(function(t){for(var e=r[Symbol.iterator](),n=0,o=e.next();!o.done&&1!==t.act(o.value,n);o=e.next(),n++);})},t.object=function(r,e){return void 0===e&&(e=!0),new t(function(t){for(var n in r)if(!e||r.hasOwnProperty(n))switch(t.act(r[n],n)){case 1:return;case 2:delete r[n];break;case 3:r[n]=t.replaceWith}})},t.linked=function(r,e,n,o,i){return i||(i=function(t){return r(t)}),function(u,a){return void 0===a&&(a=!0),new t(function(t){for(var c=u,f=e(u);f&&f!==u;){var s=e(f),p=!1;switch(t.act(r(f),i(f))){case 1:return;case 2:if(n)n(f,c),p=!0;else if(a)throw new Error("remove is required for linked list iteration");break;case 3:if(o)o(f,t.replaceWith);else if(a)throw new Error("replace is required for linked list iteration")}p||(c=f),f=s}})}},t.tree=function(r,e,n,o){o||(o=function(t){return r(t)});var i=t.empty(),u=function(r){var n=e(r);return n?Array.isArray(n)?t.array(n):n:i},a=function(t,e,i,u,a){switch(e.act(r(t),o(t))){case 1:return a&&a.stop(),!1;case 3:if(n)n(t,e.replaceWith);else if(i)throw new Error("replaceValue is required when replacing a value in a tree");break;case 2:if(a)a.remove();else if(i&&u)throw new Error("remove is not supported for breadth-first iteration")}return!0},c=function(t,r,e,n){return!!a(t,r,e,!1,n)&&!u(t).iterate(function(t,n,o){return c(t,r,e,o)}).isStopped()};return function(r,e,n){return void 0===e&&(e=!0),void 0===n&&(n=!0),new t(function(t){return e?c(r,t,n):function(t,r,e){var n=[];for(n.push(t);n.length>0;){var o=n.shift();if(!a(o,r,e,!0))break;u(o).array(n)}}(r,t,n)})}},t.join=function(){for(var r=[],e=0;e<arguments.length;e++)r[e]=arguments[e];return new t(function(t){var e,o,i=r.map(n.h);try{for(var a=u(i),c=a.next();!c.done;c=a.next()){var f=c.value;if(f.iterate(function(r,e,n){switch(t.act(r,e)){case 2:n.remove();break;case 1:n.stop();break;case 3:n.replace(t.replaceWith)}}),1===f.action)return}}catch(t){e={error:t}}finally{try{c&&!c.done&&(o=a.return)&&o.call(a)}finally{if(e)throw e.error}}})},t.empty=function(){return new t(function(t){})},t.EQUALS_STRICT=function(t,r){return t===r},t.EQUALS_LOOSE=function(t,r){return t==r},t}()},function(t,r){},function(t,r,e){"use strict";e.r(r);var n=e(0);e.d(r,"Generators",function(){return n.a}),e.d(r,"iterate",function(){return n.h}),e.d(r,"compare",function(){return n.b}),e.d(r,"equals",function(){return n.c}),e.d(r,"getNumberComparator",function(){return n.f}),e.d(r,"getStringComparator",function(){return n.g}),e.d(r,"getDateComparator",function(){return n.d}),e.d(r,"getDateEquality",function(){return n.e});var o=e(2);for(var i in o)["Generators","iterate","compare","equals","getNumberComparator","getStringComparator","getDateComparator","getDateEquality","default"].indexOf(i)<0&&function(t){e.d(r,t,function(){return o[t]})}(i);var u=e(1);e.d(r,"Iterate",function(){return u.a}),r.default=n.h}])}); | ||
//# sourceMappingURL=iteratez.js.map |
{ | ||
"name": "iteratez", | ||
"version": "0.0.1", | ||
"version": "1.0.0", | ||
"description": "A simple yet powerful iterator that allows iteration for even the most complex types.", | ||
"main": "dist/iteratez.js", | ||
"mainName": "iz", | ||
"typings": "typings/index.d.ts", | ||
@@ -8,0 +7,0 @@ "repository": { |
331
README.md
# iteratez | ||
A simple yet powerful iterator for simple and complex types. Out of the box you can iterate over arrays, objects, and trees, but you can provide iteration capabilites to your own code no matter how complex the type (dynamically calculated, etc). | ||
A powerful functional iterator, transformer, and mutator. | ||
The iterator is lazy, so you can chain `where`/`map`/`reverse`/`exclude`/`intersect`/`sorted`/`unique`/`duplicates`/`take`/`skip`/`drop`/`append`/`prepend`/`gt`/`gte`/`lt`/`lte`s and iteration is not done until you `empty`/`has`/`contains`/`first`/`last`/`count`/`list`/`object`/`reduce`/`min`/`max`/`iterate`/`erase`/`overwrite`/`extract`/etc. | ||
Out of the box you can iterate over arrays, objects, trees, sets, maps, linked-lists, iterables - and you can provide iteration capabilites to your own code no matter how complex the type (dynamically calculated, etc). | ||
### Features | ||
\* = you can pass a where function in to test the items | ||
The iterator is lazy, so you can chain "views" and iteration is not done until you perform "operations" or "mutations" to the underlying source. | ||
- Array, object, and tree iterators out of the box. | ||
- Stop the iterator at any time (and optionally return a result) | ||
- If the underlying source supports it, remove an item. | ||
- If the underlying source supports it, replace an item. | ||
- \* Check to see if an iterator is empty. | ||
- \* Count the number of items. | ||
- \* Get the first item. | ||
- \* Get the last item. | ||
- \* Convert the items into an array. | ||
- \* Convert the items into an object given a function to obtain a key from an item. | ||
- Remove all items from the source that don't pass a test. | ||
- Return a new iterator for all the items that pass a test. | ||
- Reduce the values in the iterator down to a single value. | ||
- Map the items in the iterator to a new iterator. | ||
- Iterate over only the first `X` items. | ||
- Skip over the first `X` items. | ||
- Iterate over the items with a callback. | ||
- Append one or more iterators to the end of the current one. | ||
- Prepend one or more iterators to the beginning of the current one. | ||
- Return an iterator that goes in reverse. | ||
- Custom iterator. | ||
## Features | ||
- Array, object, tree, set, map, linked-list, and iterables out of the box. | ||
- Iteration is lazy, so iteration is only done when it absolutely needs to be. | ||
- Some [operations](operations) can exit early and cease iteration saving time and resources. | ||
- When iterating, you can stop at any time. | ||
- If the underlying source supports it, [remove](mutations) an item. | ||
- If the underlying source supports it, [replace](mutations) an item. | ||
- You can chain [views](views) which don't cause iteration until an [operation](operations) or [mutation](mutations) are called. | ||
- You can call [mutations](mutations) to affect the underlying source. | ||
- You can call [operations](operations) to iterate and produce a result. | ||
- Create your own iterator. | ||
You can see all of these features in the examples below. | ||
### Examples | ||
The example is in Typescript, but iterator is available as `iz.Iterate` in JS | ||
### Views | ||
Returns an iterator... | ||
- `where`: for a subset of the items. | ||
- `not`: for a subset of the items that don't pass test (opposite of where). | ||
- `transform`: that transforms the items to another type. | ||
- `reverse`: that iterates over the items in reverse order. | ||
- `exclude`: that excludes items found in another iterator. | ||
- `intersect`: that has common items in another iterator. | ||
- `sorted`: that is sorted based on some comparison. | ||
- `shuffle`: that is randomly ordered. | ||
- `unique`: that has only unique values. | ||
- `duplicates`: that has all the duplicate values. | ||
- `readonly`: that ignores mutations. | ||
- `keys`: only for the keys of the items (replace not supported). | ||
- `values`: only for the values of the items (new key is index based). | ||
- `take`: that only iterates over the first X items. | ||
- `skip`: that skips the first X items. | ||
- `drop`: that drops off the last X items. | ||
- `append`: that is the original iterator + one or more iterators specified. | ||
- `prepend`: that is one or more iterators specified + the original iterator. | ||
- `gt`: that only has items greater than a value. | ||
- `gte`: that only has items greater than or equal to a value. | ||
- `lt`: that only has items less than a value. | ||
- `lte`: that only has items less than or equal to a value. | ||
- `fork`: that is this, but allows a function to perform fork operations | ||
- `split`: Splits the items into two iterators (pass/fail) based on a condition. | ||
- `unzip`: Splits the view into two iterates (keys/values). | ||
### Mutations | ||
- `delete`: Removes items in the view from the source. | ||
- `overwrite`: Replaces items in the view from the source. | ||
- `extract`: Removes items in the view from the source and returns a new iterator with the removed items. | ||
### Operations | ||
- `empty`: Determines if view contains zero items. | ||
- `has`: Determines if the view contains any items. | ||
- `contains`: Determines if the view contains a specific item. | ||
- `first`: Gets the first item in the view. | ||
- `last`: Gets the last item in the view. | ||
- `count`: Counts the number of items in the view. | ||
- `array`: Builds an array of the items in the view. | ||
- `set`: Builds a Set of the items in the view. | ||
- `object`: Builds an object of the items in the view. | ||
- `entries`: Builds an array of `[key, value]` in the view. | ||
- `map`: Builds a Map of the items and keys in the view. | ||
- `group`: Builds an object of item arrays grouped by a value derived from each item. | ||
- `reduce`: Reduces the items in the view down to a single value. | ||
- `min`: Returns the minimum item in the view. | ||
- `max`: Returns the maximum item in the view. | ||
- `iterate`: Invokes a function for each item in the view. | ||
- `copy`: Copies the items in the view and returns a new iterator. | ||
### Comparison Logic | ||
The following chainable functions define how items should be compared. | ||
- `numbers`: Set number comparison logic to the iterator. | ||
- `strings`: Set string comparison logic to the iterator. | ||
- `dates`: Set date comparison logic to the iterator. | ||
- `desc`: Reverses the comparison logic. | ||
- `withEquality`: Set a custom equality function. | ||
- `withComparator`: Set a custom comparison function. | ||
### Other Functions | ||
The following static functions exist to help iterate simple sources: | ||
- `Iterate.array`: Iterates an array. | ||
- `Iterate.object`: Iterates the properties of an object, optionally just the properties explicitly set on the object. | ||
- `Iterate.tree`: Iterates trees. | ||
- `Iterate.linked`: Iterates linked-lists. | ||
- `Iterate.map`: Iterates Maps | ||
- `Iterate.set`: Iterates Sets | ||
- `Iterate.join`: Returns an iterator that iterates over one or more iterators. | ||
- `Iterate.zip`: Combines a key iterator and value iterator into one. | ||
- `Iterate.empty`: Iterates nothing. | ||
- `Iterate.entries`: Iterates an array of `[key, value]` entries. | ||
- `Iterate.iterable`: Iterates any collection that implements iterable. | ||
- `Iterate.hasEntries`: Iterates any object which has the `entries()` iterator. | ||
## Examples | ||
The example is in Typescript, but iterator is available as `iz.Iterate` and the function whic dynamically returns an iterator is `iz.iterate` or simply `iz` in JS | ||
```typescript | ||
import { Iterate } from 'iteratez'; | ||
import iz, { Iterate } from 'iteratez'; | ||
// Creating an iterator | ||
let source = Iterate.array([1, 5, 7, 9, 10]); | ||
let source = Iterate.object({ | ||
let source = iz([1, 5, 7, 9, 10]); | ||
let source = iz({ | ||
name: 'ClickerMonkey', | ||
age: 30 | ||
}); | ||
let source = iz('string'); // each character | ||
let source = iz(...source); // anything iterable | ||
let source = iz([['key', 'value'], ['key', 'value']]); // [key, value] tuples | ||
let source = iz(new Map()); // Map<K, V> | ||
let source = iz(new Set()); // Set<T> | ||
let source = iz(ReadonlyArray | ReadonlyMap | ReadonlySet | Int8Array | ...) // something that has entries() function | ||
let source = Iterate.tree( ... )(head); | ||
let source = Iterate.linked( ... )(head); | ||
let source = yourSource.yourIteratorGenerator(); | ||
@@ -51,3 +127,3 @@ | ||
// Stop | ||
source.iterate((item, iter) => { | ||
source.iterate((item, itemKey, iter) => { | ||
if (someCondition(item)) { | ||
@@ -62,7 +138,6 @@ iter.stop(42) | ||
// Remove | ||
// - if the source is an array, it removes it from the array | ||
// - if the source is an object, it removes the property | ||
// - if the source is a sequential collection, it's removed from the sequence (array, object, etc) | ||
// - if the source is a tree, it removes it from the tree including it's children | ||
// - otherwise, up to the custom source | ||
source.iterate((item, iter) => { | ||
source.iterate((item, itemKey, iter) => { | ||
if (someCondition(item)) { | ||
@@ -74,3 +149,3 @@ iter.remove(); | ||
// Replace | ||
source.iterate((item, iter) => { | ||
source.iterate((item, itemKey, iter) => { | ||
if (someCondition(item)) { | ||
@@ -90,10 +165,16 @@ iter.replace(replacement); | ||
let count = source.count(); // number | ||
let array = source.list(); // T[] | ||
let sameArray = source.list(existingArray); // T[] | ||
let personById = source.object(item => item.id); // { [item.id]: item } | ||
let sameObject = source.object(item => item.id, existingObject); | ||
let min = source.min(Iterate.COMPARE_NUMBER); // T | ||
let max = source.max(Iterate.COMPARE_NUMBER); // T | ||
let array = source.array(); // T[] | ||
let array = source.array(dest); // T[] | ||
let set = source.set(); // Set<T> | ||
let object = source.object(item => item.id); // { [item.id]: item } | ||
let object = source.object(item => item.id, dest); | ||
let entries = source.entries(): // Array<[K, T]> | ||
let map = source.map(); // Map<T, K> | ||
let group = source.group(item => item.age); // { [age]: T[] } | ||
let reduced = source.reduce(R, (T, R) => R); // R | ||
let min = source.min(); // T | ||
let max = source.max(); // T | ||
let copy = source.copy(): // Iterate<T> | ||
// ============ Modifiers ============ | ||
// ============ Mutations ============ | ||
// These are at the end of a chain of views and they | ||
@@ -103,6 +184,6 @@ // take the items in the current iterator and affects the | ||
source.erase(); // removes all items in iterator | ||
source.where(x => x.id).erase(); // remove items without an ID | ||
source.delete(); // removes all items in iterator | ||
source.where(x => x.id).delete(); // remove items without an ID | ||
source.extract(); // does an erase and returns a new iterator with the removed items | ||
source.extract(); // does a delete and returns a new iterator with the removed items | ||
@@ -115,7 +196,8 @@ source.overwrite(42); // replaces all items in iterator | ||
// it only on the items in the iterator at that point. If you call | ||
// a modifier then it changes the underlying source but only on the | ||
// a mutation then it changes the underlying source but only on the | ||
// items in the view. | ||
source.where(x => x.age > 0); // items that past test | ||
source.map(x => x.name); // items mapped to a new type | ||
source.not(x => x.age > 0); // items that don't pass test | ||
source.transform(x => x.name); // items transformed to a new type | ||
source.reverse(); // items in reverse | ||
@@ -125,4 +207,8 @@ source.exclude(anotherSource); // not shared items | ||
source.sorted(comparator?); // sorted by a comparator | ||
source.shuffle(times?); // randomly orders | ||
source.unique(equality?); // unique items only | ||
source.duplicates(onlyOnce?); // duplicate items only | ||
source.readonly(); // all subsequent mutations are ignored | ||
source.keys(); // just the keys (index based), delete mutation works | ||
source.values(); // just the values (index based) | ||
source.take(10); // first 10 items | ||
@@ -137,2 +223,7 @@ source.skip(5); // after first 5 items | ||
source.lte(value, comparator?); // all items less/equal to value | ||
source.fork(f => f.where(x => !!x.male).delete()); // fork operation | ||
source.split(x => x.male); // { pass, fail } | ||
source.split(x => x.male, (pass, fail) => {}): // two iterators | ||
source.unzip(); // { keys, values } | ||
source.unzip((keys, values) => {}); // two iterators | ||
@@ -148,20 +239,26 @@ // ============ Logic ============ | ||
// Pre-defined logic | ||
source.numbers(ascending?, nullsFirst?); // number logic | ||
source.strings(sensitive?, ascending?, nullsFirst?); // string logic | ||
source.dates(equalityTimespan?, utc?, ascending?, nullsFirst?); // date logic | ||
source.desc(); // reverse comparison logic | ||
// ============ Examples ============ | ||
// Views ending with an operation or modifier. | ||
// Views ending with an operation or mutation. | ||
source.duplicates().has(); // has duplicates? | ||
source.duplicates().erase(); // remove duplicates | ||
source.duplicates().delete(); // remove duplicates | ||
source.where(x => x.age < 18).extract(); // remove < 18yo | ||
source.sorted().skip(5).take(10).list(); // sort, get 5->15 as array | ||
source.sorted().skip(5).take(10).array(); // sort, get 5->15 as array | ||
// Map to a new iterator, but support replacement | ||
source.map<string>( | ||
// maps items to new type | ||
source.transform<string>( | ||
// transforms items to new type | ||
item => item.name, | ||
// if replace is called un a subsequent iteration, how do we take the mapped value and apply it back to the original item? | ||
// if replace is called un a subsequent iteration, how do we take the transformed value and apply it back to the original item? | ||
(replaceWith, current, item) => { | ||
item.name = replaceWith; | ||
} | ||
).iterate((name, iter) => { | ||
).iterate((name, key, iter) => { | ||
// Make all names uppercase in the most obtuse way possible | ||
@@ -172,3 +269,3 @@ iter.replace(name.toUpperCase()); | ||
// Iterate with a callback | ||
source.iterate((item, iter) => { | ||
source.iterate((item, key, iter) => { | ||
// iter.remove(); | ||
@@ -179,14 +276,46 @@ // iter.stop(withResult); | ||
// Tree iteration | ||
// ============ Linked List ============ | ||
// You can have any structure you wish | ||
interface Node<T> { | ||
value: T | ||
children?: Node[] | ||
next?: Node<T> | ||
} | ||
const linkedIterator = Iterate.linked<Node<string>, string>( | ||
// get value from a node | ||
(node) => node.value, | ||
// get next node | ||
(node) => node.next, | ||
// remove the node (optional) | ||
(node, prev) => prev.next = node.next, | ||
// replace value (optional) | ||
(node, value) => node.value = value, | ||
// if you want a key different than the value, specify this | ||
(node) => node.value.length | ||
); | ||
const head: Node<string> = ...; | ||
// for all nodes which have a value that passes the regex... | ||
// - sort them by value (asending), remove the top 5 | ||
// - convert the unremoved unsorted nodes into an array | ||
linkedIterator(head) | ||
.where(x => /regex/.test(x)) | ||
.fork(f => f.strings().sorted().take(5).delete()) | ||
.list(); | ||
// ============ Tree ============ | ||
interface Node<T> { | ||
value: T | ||
children?: Node<T>[] | ||
} | ||
// You create an iterator ready for nodes. | ||
const treeIterator = Iterate.tree<Node<string>, string>( | ||
// get a value from a node | ||
node => node.value, | ||
(node) => node.value, | ||
// get children from a node (can return an array, another iterator, undefined, or null) | ||
node => node.children, | ||
(node) => node.children, | ||
// if replace is called, apply the value (this is optional) | ||
@@ -199,14 +328,29 @@ (node, value) => node.value = value | ||
// Iterate depth-first and convert to an array | ||
const depthFirstList = treeIterator(head).list(); | ||
const depthFirstList = treeIterator(head).array(); | ||
// Iterate breadth-first and convert to an array | ||
const breadthFirstList = treeIterator(head, false).list(); | ||
const breadthFirstList = treeIterator(head, false).array(); | ||
``` | ||
// CUSTOM ITERATOR | ||
function getDateIterator (start: Date, max: number): Iterate<Date> | ||
## Custom Iterators | ||
You can add your own iterators to pick up your own types. If you are not using | ||
TypeScript you can ignore and remove the types. | ||
```typescript | ||
// Lets assume we have a type which is a [Date, number] tuple | ||
// which represents a range of dates. | ||
type DateRange = [Date, number]; | ||
// First we create an iterator given the Range | ||
// The generic arguments for Iterate<T, K, S> represent: | ||
// - T = the value being iterated | ||
// - K = the key type | ||
// - S = the source type being iterated | ||
function getDateIterator ([start, max]: DateRange) | ||
{ | ||
return new Iterate<Date>(iter => | ||
return new Iterate<Date, number, DateRange>(iter => | ||
{ | ||
// This function is called when an operation (ex: list) is called on iter | ||
// This function is called when an operation or mutation is called on iter | ||
// You should iterate over your items and respond to the action requested | ||
@@ -216,8 +360,8 @@ // In this example, if iterateNode returns false, stop all iteration | ||
for (let i = 0; i < max; i++) | ||
for (let key = 0; key < max; key++) | ||
{ | ||
// Dates are not immutable, don't want the iterator to mess this up. | ||
const copy = new Date(curr.getTime()); | ||
const value = new Date(curr.getTime()); | ||
switch (iter.act(copy)) | ||
switch (iter.act(value, key)) | ||
{ | ||
@@ -239,2 +383,3 @@ // stop all iteration | ||
// the next date in the sequence | ||
curr.setDate(curr.getDate() + 1); | ||
@@ -245,8 +390,48 @@ } | ||
// Get a list of 7 days starting 5 days from now. | ||
const week5DaysFromNow = getDateIterator(new Date(), 100).skip(5).limit(7).list(); | ||
``` | ||
// Now that we have an iterator generator, if we wanted to we could | ||
// autmatically have the library detect my custom type and call my custom | ||
// iterator. | ||
import { Generators } from 'iteratez'; | ||
## TODO | ||
// We need to detect our custom type. s is DateRange is just syntactical sugar | ||
function isDateRange(s: any): s is DateRange { | ||
return Array.isArray(s) | ||
&& s.length === 2 | ||
&& s[0] instanceof Date | ||
&& typeof s[1] === 'number' | ||
; | ||
} | ||
- Async iterators | ||
// Add a generator detection to the beginning of the list | ||
// It must return false if it's not a valid source. | ||
Generators.unshift(s => isDateRange(s) ? getDateIterator(s) : false); | ||
// Now when we do this... | ||
const dateIterator = iz([new Date(), 10]); | ||
// We have our iterator and can do anything we want with it. | ||
const dates = dateIterator.array(); | ||
// BONUS! | ||
// If we want the iz function to return a type Iterator (like Iterate<Date, number, DateRange>) | ||
// we can add our own iteratez.d.ts file like this: | ||
// <iteratez.d.ts> | ||
import { Iterate } from 'iteratez'; | ||
export * from 'iteratez'; | ||
declare module 'iteratez' | ||
{ | ||
// add the function overload | ||
export function iterate (range: DateRange): Iterate<Date, number, DateRange> | ||
} | ||
// </iteratez.d.ts> | ||
// then instead of this everywhere: | ||
import iz from 'iteratez'; | ||
// we can do this | ||
import iz from './path/to/my/iteratez.d'; | ||
const dateIterator = iz([new Date(), 3]); // Iterate<Date, number, DateRange> magic! | ||
``` |
export * from './types'; | ||
export * from './functions'; | ||
export * from './IterateAction'; | ||
export * from './Iterate'; | ||
import { iterate } from './functions'; | ||
export default iterate; |
import { IterateAction } from "./IterateAction"; | ||
import { IterateCallback, IterateCompare, IterateEquals, IterateFilter, IterateSource } from "./types"; | ||
import { GetKeyFor, GetValueFor, HasEntries, IterateCallback, IterateCompare, IterateEquals, IterateFilter, IterateSource, IterateSourceType, IterateSourceTypeKey } from "./types"; | ||
/** | ||
@@ -9,26 +9,31 @@ * A class that allows an iteratable source to be iterated any number of times. | ||
* - **View**: produces a new iterator, the original iterator is not affected. | ||
* - **Modifier**: modifies the source based on the values in the current iterator. | ||
* - **Mutation**: modifies the source based on the values in the current iterator. | ||
* | ||
* The **View** functions do not iterate over the source, the iterator they | ||
* return does not iterate over the source until an **Operation** or | ||
* **Modifier** function are called on it. | ||
* **Mutation** function are called on it. | ||
* | ||
* **Operations** | ||
* - `empty`: Determines whether the source contains no items. | ||
* - `has`: Determines whether the source contains any items. | ||
* - `contains`: Determines if the source contains a specific item. | ||
* - `first`: Gets the first item in the source. | ||
* - `last`: Gets the last item in the source. | ||
* - `count`: Counds the number of items in the source. | ||
* - `list`: Builds a list of the items in the source. | ||
* - `object`: Builds an object of the items in the source. | ||
* - `reduce`: Reduces the items in the source down to a single value. | ||
* - `min`: Returns the minimum item in ths source. | ||
* - `max`: Returns the maximum item in ths source. | ||
* - `iterate`: Invokes a function for each item in the source. | ||
* - `empty`: Determines whether the view contains no items. | ||
* - `has`: Determines whether the view contains any items. | ||
* - `contains`: Determines if the view contains a specific item. | ||
* - `first`: Gets the first item in the view. | ||
* - `last`: Gets the last item in the view. | ||
* - `count`: Counds the number of items in the view. | ||
* - `array`: Builds an array of the items in the view. | ||
* - `set`: Builds a Set of the items in the view. | ||
* - `object`: Builds an object of the items in the view. | ||
* - `entries`: Builds an array of `[key, value]` in the view. | ||
* - `map`: Builds a Map of the items and keys in the view. | ||
* - `group`: Builds an object of item arrays grouped by a value derived from each item. | ||
* - `reduce`: Reduces the items in the view down to a single value. | ||
* - `min`: Returns the minimum item in the view. | ||
* - `max`: Returns the maximum item in the view. | ||
* - `iterate`: Invokes a function for each item in the view. | ||
* - `copy`: Copies the items in the view and returns a new iterator. | ||
* | ||
* **Modifiers** | ||
* - `erase`: Removes items in the iterator from the source. | ||
* - `overwrite`: Replaces items in the iterator from the source. | ||
* - `extract`: Removes items in the iterator from the source and returns a new iterator with the removed items. | ||
* **Mutations** | ||
* - `delete`: Removes items in the view from the source. | ||
* - `overwrite`: Replaces items in the view from the source. | ||
* - `extract`: Removes items in the view from the source and returns a new iterator with the removed items. | ||
* | ||
@@ -38,3 +43,4 @@ * **Views** | ||
* - `where`: for a subset of the items. | ||
* - `map`: that maps the items to another value. | ||
* - `not`: for a subset of the items (opposite of where). | ||
* - `transform`: that transforms the items to another type. | ||
* - `reverse`: that iterates over the items in reverse order. | ||
@@ -44,9 +50,13 @@ * - `exclude`: that excludes items found in another iterator. | ||
* - `sorted`: that is sorted based on some comparison. | ||
* - `shuffle`: that is randomly ordered. | ||
* - `unique`: that has only unique values. | ||
* - `duplicates`: that has all the duplicate values. | ||
* - `take`: Returns an iterator that only iterates over the first X items. | ||
* - `skip`: Returns an iterator that skips the first X items. | ||
* - `drop`: Returns an iterator that drops off the last X items. | ||
* - `append`: Returns an iterator that is the original iterator + one or more iterators specified. | ||
* - `prepend`: Returns an iterator that is one or more iterators specified + the original iterator. | ||
* - `readonly`: that ignores mutations. | ||
* - `keys`: only for the keys of the items (replace not supported). | ||
* - `values`: only for the values of the items (new key is index based). | ||
* - `take`: that only iterates over the first X items. | ||
* - `skip`: that skips the first X items. | ||
* - `drop`: that drops off the last X items. | ||
* - `append`: that is the original iterator + one or more iterators specified. | ||
* - `prepend`: that is one or more iterators specified + the original iterator. | ||
* - `gt`: that only has items greater than a value. | ||
@@ -56,41 +66,41 @@ * - `gte`: that only has items greater than or equal to a value. | ||
* - `lte`: that only has items less than or equal to a value. | ||
* - `fork`: that is this, but allows a function to perform fork operations | ||
* - `split`: Splits the items into two iterators (pass/fail) based on a condition. | ||
* - `unzip`: Splits the view into two iterates (keys/values). | ||
* | ||
* The following functions are used to control comparison logic | ||
* | ||
* - `numbers`: Set number comparison logic to the iterator. | ||
* - `strings`: Set string comparison logic to the iterator. | ||
* - `dates`: Set date comparison logic to the iterator. | ||
* - `desc`: Reverses the comparison logic. | ||
* - `withEquality`: Set a custom equality function. | ||
* - `withComparator`: Set a custom comparison function. | ||
* | ||
* The following static functions exist to help iterate simple sources: | ||
* | ||
* - `array`: Iterates an array, optionally reverse. | ||
* - `array`: Iterates an array. | ||
* - `object`: Iterates the properties of an object, optionally just the properties explicitly set on the object. | ||
* - `empty`: An iterator with no items | ||
* - `tree`: Iterates trees. | ||
* - `linked`: Iterates linked-lists. | ||
* - `map`: Iterates Maps | ||
* - `set`: Iterates Sets | ||
* - `hasEntries`: Iterates any object which has the `entries()` iterator. | ||
* - `empty`: Iterates nothing. | ||
* - `iterable`: Iterates any collection that implements iterable. | ||
* - `join`: Returns an iterator that iterates over one or more iterators. | ||
* - `entries`: Iterates an array of `[key, value]` entries. | ||
* - `zip`: Combines a key iterator and value iterator into one. | ||
* | ||
* @typeparam T The type of item being iterated. | ||
*/ | ||
export declare class Iterate<T> { | ||
export declare class Iterate<T, K, S> { | ||
/** | ||
* An equality check by reference. | ||
*/ | ||
static EQUALS_STRICT: IterateEquals<unknown>; | ||
static EQUALS_STRICT: IterateEquals<any, any>; | ||
/** | ||
* An equality check by value. | ||
*/ static EQUALS_LOOSE: IterateEquals<unknown>; | ||
*/ static EQUALS_LOOSE: IterateEquals<any, any>; | ||
/** | ||
* An equality check for case sensitive strings. | ||
*/ | ||
static EQUALS_STRING_SENSITIVE: IterateEquals<string>; | ||
/** | ||
* An equality check for case insensitive strings. | ||
*/ | ||
static EQUALS_STRING_INSENSITIVE: IterateEquals<string>; | ||
/** | ||
* A comparator for numbers. | ||
*/ | ||
static COMPARE_NUMBER: IterateCompare<number>; | ||
/** | ||
* A comparator for strings. | ||
*/ | ||
static COMPARE_STRING: IterateCompare<string>; | ||
/** | ||
* A comparator for Dates. | ||
*/ | ||
static COMPARE_DATE: IterateCompare<Date>; | ||
/** | ||
* A result of the iteration passed to [[Iterate.stop]]. | ||
@@ -110,3 +120,3 @@ */ | ||
*/ | ||
callback: IterateCallback<T, any>; | ||
callback: IterateCallback<T, K, S, any>; | ||
/** | ||
@@ -132,3 +142,3 @@ * The source of iterable items. This allows the iteration over any type of | ||
*/ | ||
constructor(source: IterateSource<T>, parent?: Iterate<T>); | ||
constructor(source: IterateSource<T, K, S>, parent?: Iterate<T, any, any>); | ||
/** | ||
@@ -139,3 +149,3 @@ * Returns a clone of this iterator with the same source. This is necessary | ||
*/ | ||
clone(): Iterate<T>; | ||
clone(): Iterate<T, K, S>; | ||
/** | ||
@@ -147,3 +157,3 @@ * Passes the given item to the iterator callback and returns the action | ||
*/ | ||
act(item: T): IterateAction; | ||
act(item: T, key: K): IterateAction; | ||
/** | ||
@@ -174,3 +184,3 @@ * Stops iteration and optionally sets the result of the iteration. | ||
*/ | ||
withEquality(equality: IterateEquals<T>): this; | ||
withEquality(equality: IterateEquals<T, K>): this; | ||
/** | ||
@@ -183,4 +193,44 @@ * Sets the comparison logic for this iterator and subsequent views. If this | ||
*/ | ||
withComparator(comparator: IterateCompare<T>): this; | ||
withComparator(comparator: IterateCompare<T, K>): this; | ||
/** | ||
* Applies the logic to the iterator. | ||
* | ||
* @param comparator The comparison logic. | ||
* @param equality The equality logic if the comparison logic won't suffice. | ||
*/ | ||
private withLogic; | ||
/** | ||
* Applies number equality and comparison logic to this iterator and | ||
* subsequent ones. | ||
* | ||
* @param ascending If the numbers should be in ascending order. | ||
* @param nullsFirst If non-number values should be ordered first. | ||
*/ | ||
numbers(ascending?: boolean, nullsFirst?: boolean): this; | ||
/** | ||
* Applies string equality and comparison logic to this iterator and | ||
* subsequent ones. | ||
* | ||
* @param sensitive If equality logic should be case sensitive. | ||
* @param ascending If the strings should be in ascending order. | ||
* @param nullsFirst If non-strings values should be ordered first. | ||
*/ | ||
strings(sensitive?: boolean, ascending?: boolean, nullsFirst?: boolean): this; | ||
/** | ||
* Applies date equality and comparison logic to this iterator and | ||
* subsequent ones. | ||
* | ||
* @param equalityTimespan This defines which timespan to dates can lie on | ||
* to be considered equal. By default they have to have the same | ||
* millisecond. If you want dates equal by the following timespans use | ||
* these values: Second = 1000, Minute = 60000, Hour = 3600000, | ||
* Day = 86400000, Week = 604800000 (approx) | ||
* @param utc If when comparing timespans, if we should look at their UTC | ||
* date/time OR if they should be changed to their relative times based | ||
* on their timezone. | ||
* @param ascending If the dates should be in ascending order (oldest first). | ||
* @param nullsFirst If non-date values should be ordered first. | ||
*/ | ||
dates(equalityTimespan?: number, utc?: boolean, ascending?: boolean, nullsFirst?: boolean): this; | ||
/** | ||
* Reverses the comparator on this iterator and subsequent views. If this | ||
@@ -191,3 +241,3 @@ * iterator does not have a comparator this has no affect. | ||
*/ | ||
desc(comparator?: IterateCompare<T>): this; | ||
desc(comparator?: IterateCompare<T, K>): this; | ||
/** | ||
@@ -199,3 +249,3 @@ * Gets the equality logic desired, optionally overriding the one specified | ||
*/ | ||
getEquality(equalityOverride?: IterateEquals<T>): IterateEquals<T>; | ||
getEquality(equalityOverride?: IterateEquals<T, K>): IterateEquals<T, K>; | ||
/** | ||
@@ -207,3 +257,3 @@ * Gets the comparison logic desired, optionally overriding the one specified | ||
*/ | ||
getComparator(comparatorOverride?: IterateCompare<T>): IterateCompare<T>; | ||
getComparator(comparatorOverride?: IterateCompare<T, K>): IterateCompare<T, K>; | ||
/** | ||
@@ -227,3 +277,3 @@ * An operation that determines whether this iterator is empty. | ||
*/ | ||
contains(value: T, equality?: IterateEquals<T>): boolean; | ||
contains(value: T, equality?: IterateEquals<T, K>): boolean; | ||
/** | ||
@@ -248,3 +298,3 @@ * An operation that counts the number of items in the iterator. | ||
/** | ||
* An operation that builds a list of items from the source. | ||
* An operation that builds an array of items from the source. | ||
* | ||
@@ -254,4 +304,11 @@ * @param out The array to place the items in. | ||
*/ | ||
list(out?: T[]): T[]; | ||
array(out?: T[]): T[]; | ||
/** | ||
* An operation that builds an array of [key, item] entries from this view. | ||
* | ||
* @param out The array to place the entries in. | ||
* @returns The reference to `out` which has had entries added to it. | ||
*/ | ||
entries(out?: Array<[K, T]>): Array<[K, T]>; | ||
/** | ||
* An operation that builds an object of items from the iterator keyed by a | ||
@@ -268,2 +325,27 @@ * result returned by a `getKey` function. | ||
/** | ||
* An operation that builds a Set of items from the source. | ||
* | ||
* @param out The Set to place the items in. | ||
* @returns The reference to `out` which has had items added to it. | ||
*/ | ||
set(out?: Set<T>): Set<T>; | ||
/** | ||
* An operation that builds a Map of key-value pairs from the source. | ||
* | ||
* @param out The Map to place the items in. | ||
* @returns The reference to `out` which has had items added to it. | ||
*/ | ||
map(out?: Map<K, T>): Map<K, T>; | ||
/** | ||
* An operation that returns an object with arrays of items where the | ||
* property of the object is a key returned by a function. | ||
* | ||
* @param by A function to get the key from an item. | ||
* @param out The object to add groups to. | ||
* @returns The reference to `out` which has had items added to it. | ||
*/ | ||
group<G extends { | ||
[by: string]: T[]; | ||
}>(by: (item: T) => any, out?: G): G; | ||
/** | ||
* An operation that reduces all the items in the source to a single value | ||
@@ -285,3 +367,3 @@ * given the initial value and a function to convert an item and the current | ||
*/ | ||
min(comparator?: IterateCompare<T>): T; | ||
min(comparator?: IterateCompare<T, K>): T; | ||
/** | ||
@@ -293,14 +375,14 @@ * An operation that returns the maximum item in this iterator. If this | ||
*/ | ||
max(comparator?: IterateCompare<T>): T; | ||
max(comparator?: IterateCompare<T, K>): T; | ||
/** | ||
* A modifier which removes items in this iterator from the source. | ||
* A mutation which removes items in this iterator from the source. | ||
*/ | ||
erase(): this; | ||
delete(): this; | ||
/** | ||
* A modifier which removes items in this iterator from the source and | ||
* A mutation which removes items in this iterator from the source and | ||
* returns a new iterator with the removed items. | ||
*/ | ||
extract(): Iterate<T>; | ||
extract(): Iterate<T, K, Array<[K, T]>>; | ||
/** | ||
* A modifier which replaces items in this iterator from the source. | ||
* A mutation which replaces items in this iterator from the source. | ||
* | ||
@@ -311,2 +393,52 @@ * @param replacement The item to replace for all the items in this iterator. | ||
/** | ||
* Forks this view into another and returns a reference to this view. | ||
* This allows chaining of multiple views which each perform a different | ||
* operation or mutation. Forks are executed sequentially, so if one fork | ||
* performs mutations the subsequent forks will see the mutated items. | ||
* | ||
* @param forker A function which takes the iterator at this point and | ||
* performs any mutations and operations. | ||
* @returns The reference to this iterator. | ||
*/ | ||
fork(forker: (fork: this) => any): this; | ||
/** | ||
* Provides split views of the items in this iterator, one iterator gets | ||
* passed items and the other iterator gets the failed items. | ||
* | ||
* You can pass a function as a second argument which recieves two iterators | ||
* for pass and fail respectively. This will be returned in that scenario. | ||
* | ||
* If you don't pass a second function an object will be returned with two | ||
* properties: pass and fail. | ||
*/ | ||
split(pass: IterateFilter<T, K>): { | ||
pass: Iterate<T, K, S>; | ||
fail: Iterate<T, K, S>; | ||
}; | ||
split(pass: IterateFilter<T, K>, handle: (pass: Iterate<T, K, S>, fail: Iterate<T, K, S>) => any): this; | ||
/** | ||
* Unzips the view into a keys and values views. | ||
* | ||
* You can pass a function as a second argument which recieves two iterators | ||
* for keys and values respectively. This will be returned in that scenario. | ||
* | ||
* If you don't pass a second function an object will be returned with two | ||
* properties: keys and values. | ||
*/ | ||
unzip(): { | ||
keys: Iterate<K, number, S>; | ||
values: Iterate<T, number, S>; | ||
}; | ||
unzip(handle: (keys: Iterate<K, number, S>, values: Iterate<T, number, S>) => any): this; | ||
/** | ||
* Returns a view of just the keys in this view. Any mutations done to the | ||
* keys view affect the underlying source. | ||
*/ | ||
keys(): Iterate<K, number, S>; | ||
/** | ||
* Returns a view of just the values in this view. Any mutations done to the | ||
* values view affect the underlying source. | ||
*/ | ||
values(): Iterate<T, number, S>; | ||
/** | ||
* Returns a view of this iterator. This allows other views to be more DRY. | ||
@@ -319,3 +451,3 @@ * | ||
*/ | ||
view<D>(getData: () => D, shouldAct: (data: D, item: T) => any, afterAct?: (data: D, item: T, iter: Iterate<T>) => void, afterSkip?: (data: D, item: T, iter: Iterate<T>) => void): Iterate<T>; | ||
view<D>(getData: () => D, shouldAct: (data: D, item: T, key: K) => any, afterAct?: (data: D, item: T, key: K, iter: Iterate<T, K, S>) => void, afterSkip?: (data: D, item: T, key: K, iter: Iterate<T, K, S>) => void): Iterate<T, K, S>; | ||
/** | ||
@@ -326,3 +458,3 @@ * Returns a view that only returns a maximum number of items. | ||
*/ | ||
take(amount: number): Iterate<T>; | ||
take(amount: number): Iterate<T, K, S>; | ||
/** | ||
@@ -334,3 +466,3 @@ * Returns a view that skips the given number of items from the items | ||
*/ | ||
skip(amount: number): Iterate<T>; | ||
skip(amount: number): Iterate<T, K, S>; | ||
/** | ||
@@ -342,3 +474,3 @@ * Returns a view that drops the given number of items from the end of the | ||
*/ | ||
drop(amount: number): Iterate<T>; | ||
drop(amount: number): Iterate<T, K, S>; | ||
/** | ||
@@ -350,3 +482,6 @@ * Returns a view thats items are the items in this iterator followed | ||
*/ | ||
append(...iterators: Iterate<T>[]): Iterate<T>; | ||
append(...sources: S[]): Iterate<T, K, S>; | ||
append(...sources: IterateSourceTypeKey<T, K>[]): Iterate<T, K, any>; | ||
append(...sources: IterateSourceType<T>[]): Iterate<T, any, any>; | ||
append(...sources: IterateSourceType<any>[]): Iterate<any, any, any>; | ||
/** | ||
@@ -358,3 +493,6 @@ * Returns a view thats items are the items in the given iterators | ||
*/ | ||
prepend(...iterators: Iterate<T>[]): Iterate<T>; | ||
prepend(...sources: S[]): Iterate<T, K, S>; | ||
prepend(...sources: IterateSourceTypeKey<T, K>[]): Iterate<T, K, any>; | ||
prepend(...sources: IterateSourceType<T>[]): Iterate<T, any, any>; | ||
prepend(...sources: IterateSourceType<any>[]): Iterate<any, any, any>; | ||
/** | ||
@@ -365,4 +503,10 @@ * Returns a view of items in this iterator which pass a `where` function. | ||
*/ | ||
where(where: IterateFilter<T>): Iterate<T>; | ||
where(where: IterateFilter<T, K>): Iterate<T, K, S>; | ||
/** | ||
* Returns a view of items in this iterator which do NOT pass a `not` function. | ||
* | ||
* @param not The function which determines if an item should be iterated. | ||
*/ | ||
not(not: IterateFilter<T, K>): Iterate<T, K, S>; | ||
/** | ||
* Returns a view where all items are greater than the given value. | ||
@@ -374,3 +518,3 @@ * If a comparator is not on this iterator or provided an error is thrown. | ||
*/ | ||
gt(value: T, comparator?: IterateCompare<T>): Iterate<T>; | ||
gt(value: T, comparator?: IterateCompare<T, K>): Iterate<T, K, S>; | ||
/** | ||
@@ -383,3 +527,3 @@ * Returns a view where all items are greater than or equal to the given value. | ||
*/ | ||
gte(value: T, comparator?: IterateCompare<T>): Iterate<T>; | ||
gte(value: T, comparator?: IterateCompare<T, K>): Iterate<T, K, S>; | ||
/** | ||
@@ -392,3 +536,3 @@ * Returns a view where all items are less than the given value. | ||
*/ | ||
lt(value: T, comparator?: IterateCompare<T>): Iterate<T>; | ||
lt(value: T, comparator?: IterateCompare<T, K>): Iterate<T, K, S>; | ||
/** | ||
@@ -401,3 +545,3 @@ * Returns a view where all items are less than or equal to the given value. | ||
*/ | ||
lte(value: T, comparator?: IterateCompare<T>): Iterate<T>; | ||
lte(value: T, comparator?: IterateCompare<T, K>): Iterate<T, K, S>; | ||
/** | ||
@@ -410,3 +554,3 @@ * Returns a view of this iterator which does not include the items in the | ||
*/ | ||
exclude(values: Iterate<T>, equality?: IterateEquals<T>): Iterate<T>; | ||
exclude(source: IterateSourceType<T>, equality?: IterateEquals<T, any>): Iterate<T, K, S>; | ||
/** | ||
@@ -419,3 +563,3 @@ * Returns a view which has items which are in this iterator and the given | ||
*/ | ||
intersect(values: Iterate<T>, equality?: IterateEquals<T>): Iterate<T>; | ||
intersect(source: IterateSourceType<T>, equality?: IterateEquals<T, any>): Iterate<T, K, S>; | ||
/** | ||
@@ -426,3 +570,3 @@ * Returns a view which only contains unique items. | ||
*/ | ||
unique(equality?: IterateEquals<T>): Iterate<T>; | ||
unique(equality?: IterateEquals<T, K>): Iterate<T, K, S>; | ||
/** | ||
@@ -436,5 +580,13 @@ * Returns a view which only contains items that have duplicates in this | ||
*/ | ||
duplicates(onlyOnce?: boolean, equality?: IterateEquals<T>): Iterate<T>; | ||
duplicates(onlyOnce?: boolean, equality?: IterateEquals<T, K>): Iterate<T, K, S>; | ||
/** | ||
* Returns a view which requires a fully resolved list of items. The view | ||
* Returns a readonly view where mutations have no affect. | ||
*/ | ||
readonly(): Iterate<T, K, S>; | ||
/** | ||
* Returns a copy of the items in this view as a new iterator. | ||
*/ | ||
copy(): Iterate<T, K, Array<[K, T]>>; | ||
/** | ||
* Returns a view which requires a fully resolved array of items. The view | ||
* must keep track of the original item index in order to ensure removals | ||
@@ -445,3 +597,3 @@ * and replaces can be performed on the source. | ||
*/ | ||
viewResolved(onResolve: (items: T[], handleAct: (item: T, index: number) => IterateAction) => void): Iterate<T>; | ||
viewResolved(onResolve: (items: Array<[K, T]>, handleAct: (item: T, itemKey: K, index: number) => IterateAction) => void): Iterate<T, K, S>; | ||
/** | ||
@@ -452,16 +604,20 @@ * Returns a view which has the items sorted. | ||
*/ | ||
sorted(comparator?: IterateCompare<T>): Iterate<T>; | ||
sorted(comparator?: IterateCompare<T, K>): Iterate<T, K, S>; | ||
/** | ||
* Returns an view of items in this iterator and presents them in a random order. | ||
*/ | ||
shuffle(passes?: number): Iterate<T, K, S>; | ||
/** | ||
* Returns an view of items in this iterator and presents them in reverse. | ||
*/ | ||
reverse(): Iterate<T>; | ||
reverse(): Iterate<T, K, S>; | ||
/** | ||
* Returns an iterator where this iterator is the source and the returned | ||
* iterator is built from mapped items pulled from items in the source | ||
* iterator is built from transformed items pulled from items in the source | ||
* of this iterator. | ||
* | ||
* @param mapper The function which maps an item to another. | ||
* @param unmapper The function which unmaps a value when replace is called. | ||
* @param transformer The function which transforms an item to another. | ||
* @param untransformer The function which untransforms a value when replace is called. | ||
*/ | ||
map<W>(mapper: IterateCallback<T, W>, unmapper?: (replaceWith: W, current: W, item: T) => T): Iterate<W>; | ||
transform<W>(transformer: IterateCallback<T, K, S, W>, untransformer?: (replaceWith: W, current: W, item: T, key: K) => T): Iterate<W, K, S>; | ||
/** | ||
@@ -474,3 +630,3 @@ * Invokes the callback for each item in the source of this iterator. The | ||
*/ | ||
iterate(callback: IterateCallback<T, any>): this; | ||
iterate(callback: IterateCallback<T, K, S, any>): this; | ||
/** | ||
@@ -488,3 +644,3 @@ * Passes the result of the iteration to the given function if a truthy | ||
/** | ||
* Returns an iterator for the given array optionally iterating it in reverse. | ||
* Returns an iterator for the given array. | ||
* | ||
@@ -494,4 +650,56 @@ * @param items The array of items to iterate. | ||
*/ | ||
static array<T>(items: T[]): Iterate<T>; | ||
static entries<T, K>(items: Array<[K, T]>): Iterate<T, K, Array<[K, T]>>; | ||
/** | ||
* Returns an iterator for the given array. | ||
* | ||
* @param items The array of items to iterate. | ||
* @returns A new iterator for the given array. | ||
*/ | ||
static array<T>(items: T[]): Iterate<T, number, T[]>; | ||
/** | ||
* Returns an iterator for the keys and values specified. If the key and | ||
* value iterators don't have the same number of items, the returned iterator | ||
* will have the maximum pairs possible (which is the lesser of the number | ||
* of keys and values). | ||
* | ||
* If the returned iterator is mutated the given keys and values iterators | ||
* will be mutated as well. If you want to avoid that, pass in readonly | ||
* key/value iterators. | ||
* | ||
* @param keys The iterator to obtain the keys from. | ||
* @param values The iterator to obtain the values from. | ||
*/ | ||
static zip<T, K, S extends IterateSourceTypeKey<T, K, S>>(keys: S, values: S): Iterate<T, K, S>; | ||
static zip<T, K>(keys: IterateSourceTypeKey<K, number>, values: IterateSourceTypeKey<T, number>): Iterate<T, K, any>; | ||
static zip<T, K>(keys: IterateSourceType<K>, values: IterateSourceType<T>): Iterate<T, K, any>; | ||
/** | ||
* Returns an iterator for any object which has an entries() iterable. | ||
* | ||
* @param hasEntries The object with the entries() iterable. | ||
* @param onRemove The function that should handle removing a key/value. | ||
* @param onReplace The function that should handle replacing a value. | ||
*/ | ||
static hasEntries<T, K, E extends HasEntries<T, K>>(hasEntries: E, onRemove?: (entries: E, key: K, value: T) => any, onReplace?: (entries: E, key: K, value: T, newValue: T) => any): Iterate<T, K, E>; | ||
/** | ||
* Returns an iterator for the given Map. | ||
* | ||
* @param items The Map of key-value pairs to iterate. | ||
* @returns A new iterator for the given Map. | ||
*/ | ||
static map<T, K>(items: Map<K, T>): Iterate<T, K, Map<K, T>>; | ||
/** | ||
* Returns an iterator for the given Set. | ||
* | ||
* @param items The Set of items to iterate. | ||
* @returns A new iterator for the given Set. | ||
*/ | ||
static set<T>(items: Set<T>): Iterate<T, T, Set<T>>; | ||
/** | ||
* Returns an iterator for any iterable. Because iterables don't support | ||
* | ||
* @param items The iterable collection. | ||
* @returns A new iterator for the given set. | ||
*/ | ||
static iterable<T, I extends Iterable<T>>(items: I): Iterate<T, number, I>; | ||
/** | ||
* Returns an iterator for the given object optionally checking the | ||
@@ -506,4 +714,22 @@ * `hasOwnProperty` function on the given object. | ||
[key: string]: T; | ||
}, hasOwnProperty?: boolean): Iterate<T>; | ||
}, hasOwnProperty?: boolean): Iterate<T, string, { | ||
[key: string]: T; | ||
}>; | ||
/** | ||
* Returns a function for iterating over a linked-list. You pass a node to | ||
* the function returned (and whether it should be strict) and an iterator | ||
* will be returned. | ||
* | ||
* @param getValue A function which gets a value from a node. | ||
* @param getNext A function which returns the next node. When at the end | ||
* list null or undefined should be returned. | ||
* @param remove A function which handles a remove request. If this is not | ||
* specified and a remove is called and `strict` is true an error will be | ||
* thrown. | ||
* @param replaceValue A function which applies a value to a node. If this is | ||
* not specified and a replace is called and `strict` is true an error | ||
* will be thrown. | ||
*/ | ||
static linked<N, T = any, K = T>(getValue: (node: N) => T, getNext: (node: N) => N | undefined | null, remove?: (node: N, prev: N) => any, replaceValue?: (node: N, value: T) => any, getKey?: (node: N) => K): (previousNode: N, strict?: boolean) => Iterate<T, K, N>; | ||
/** | ||
* Returns a function for iterating over a tree. You pass a node to the | ||
@@ -516,6 +742,7 @@ * function returned (and whether it should perform a depth-first or | ||
* iterator which can return the children. | ||
* @param replaceValue A function which applies a value to a node. | ||
*/ | ||
static tree<N, T = any>(getValue: (node: N) => T, getChildren: (node: N) => N[] | Iterate<N> | undefined | null, replaceValue?: (node: N, value: T) => any): (startingNode: N, depthFirst?: boolean, strict?: boolean) => Iterate<T>; | ||
static tree<N, T = any, K = T>(getValue: (node: N) => T, getChildren: (node: N) => N[] | Iterate<N, any, any> | undefined | null, replaceValue?: (node: N, value: T) => any, getKey?: (node: N) => K): (startingNode: N, depthFirst?: boolean, strict?: boolean) => Iterate<T, K, N>; | ||
/** | ||
* Joins all the given iterators into a single iterator where the items | ||
* Joins all the given sources into a single iterator where the items | ||
* returned are in the same order as passed to this function. If any items | ||
@@ -525,6 +752,10 @@ * are removed from the returned iterator they will be removed from the given | ||
* | ||
* @param iterators The array of iterators to join as one. | ||
* @returns A new iterator for the given iterators. | ||
* @param sources The sources to get iterators for to join. | ||
* @returns A new iterator for the given sources. | ||
*/ | ||
static join<T>(...iterators: Iterate<T>[]): Iterate<T>; | ||
static join<T, K, S>(...sources: Iterate<T, K, S>[]): Iterate<T, K, S>; | ||
static join<S, T extends GetValueFor<S>, K extends GetKeyFor<S>>(...sources: S[]): Iterate<T, K, S>; | ||
static join<T, K>(...sources: IterateSourceTypeKey<T, K>[]): Iterate<T, K, any>; | ||
static join<T>(...sources: IterateSourceType<T>[]): Iterate<T, any, any>; | ||
static join(...sources: IterateSourceType<any>[]): Iterate<any, any, any>; | ||
/** | ||
@@ -535,3 +766,3 @@ * Returns a new iterator with no items. | ||
*/ | ||
static empty<T>(): Iterate<T>; | ||
static empty<T, K, S>(): Iterate<T, K, S>; | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
74198
10
938
1
423