Skip to content

Instantly share code, notes, and snippets.

@shaunlebron
Created March 2, 2018 03:15
Show Gist options
  • Save shaunlebron/86f702266727e95dc0b98b7740f42907 to your computer and use it in GitHub Desktop.
Save shaunlebron/86f702266727e95dc0b98b7740f42907 to your computer and use it in GitHub Desktop.
!function(r){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=r();else if("function"==typeof define&&define.amd)define([],r);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.obliterator=r()}}(function(){return function r(e,t,n){function o(a,u){if(!t[a]){if(!e[a]){var s="function"==typeof require&&require;if(!u&&s)return s(a,!0);if(i)return i(a,!0);var f=new Error("Cannot find module '"+a+"'");throw f.code="MODULE_NOT_FOUND",f}var c=t[a]={exports:{}};e[a][0].call(c.exports,function(r){var t=e[a][1][r];return o(t?t:r)},c,c.exports,r,e,t,n)}return t[a].exports}for(var i="function"==typeof require&&require,a=0;a<n.length;a++)o(n[a]);return o}({1:[function(r,e,t){var n=r("./iterator.js");e.exports=function(){var r,e=arguments,t=-1;return new n(function n(){if(!r){if(t++,t>=e.length)return{done:!0};r=e[t]}var o=r.next();return o.done?(r=null,n()):o})}},{"./iterator.js":7}],2:[function(r,e,t){function n(r,e,t,n){for(var o=0;o<n;o++)r[o]=e[t[o]]}var o=r("./iterator.js");e.exports=function(r,e){if(!Array.isArray(r))throw new Error("obliterator/combinations: first argument should be an array.");var t=r.length;if("number"!=typeof e)throw new Error("obliterator/combinations: second argument should be omitted or a number.");if(e>t)throw new Error("obliterator/combinations: the size of the subsequences should not exceed the length of the array.");if(e===t)return o.of(r.slice());var i,a=new Array(e),u=new Array(e),s=!0;for(i=0;i<e;i++)a[i]=i;return new o(function(){if(s)return s=!1,n(u,r,a,e),{value:u};if(a[e-1]++<t-1)return n(u,r,a,e),{value:u};for(i=e-2;i>=0&&a[i]>=t-(e-i);)--i;if(i<0)return{done:!0};for(a[i]++;++i<e;)a[i]=a[i-1]+1;return n(u,r,a,e),{value:u}})}},{"./iterator.js":7}],3:[function(r,e,t){e.exports=function(r,e){for(var t,n=arguments.length>1?new Array(e):[],o=0;t=r.next(),!t.done;)n[o++]=t.value;return n}},{}],4:[function(r,e,t){var n=r("./iterator.js");e.exports=function(r,e){return new n(function t(){var n=e.next();return n.done?n:r(n.value)?n:t()})}},{"./iterator.js":7}],5:[function(r,e,t){var n=r("./iterator.js");e.exports=function(r,e){if(!n.is(r))throw new Error("obliterator/foreach: invalid iterator.");if("function"!=typeof e)throw new Error("obliterator/foreach: callback is not a function.");for(var t,o=0;t=r.next(),!t.done;)e(t.value,o++)}},{"./iterator.js":7}],6:[function(r,e,t){e.exports={Iterator:r("./iterator.js"),chain:r("./chain.js"),combinations:r("./combinations.js"),consume:r("./consume.js"),filter:r("./filter.js"),forEach:r("./foreach.js"),map:r("./map.js"),match:r("./match.js"),permutations:r("./permutations.js"),powerSet:r("./power-set.js"),range:r("./range.js"),split:r("./split.js")}},{"./chain.js":1,"./combinations.js":2,"./consume.js":3,"./filter.js":4,"./foreach.js":5,"./iterator.js":7,"./map.js":8,"./match.js":9,"./permutations.js":10,"./power-set.js":11,"./range.js":12,"./split.js":13}],7:[function(r,e,t){function n(r){Object.defineProperty(this,"_next",{writable:!1,enumerable:!1,value:r}),this.done=!1}n.prototype.next=function(){if(this.done)return{done:!0};var r=this._next();return r.done&&(this.done=!0),r},"undefined"!=typeof Symbol&&(n.prototype[Symbol.iterator]=function(){return this}),n.of=function(r){var e=!1;return new n(function(){return e?{done:!0}:(e=!0,{value:r})})},n.empty=function(){var r=new n(null);return r.done=!0,r},n.is=function(r){return r instanceof n||"object"==typeof r&&null!==r&&"function"==typeof r.next},e.exports=n},{}],8:[function(r,e,t){var n=r("./iterator.js");e.exports=function(r,e){return new n(function(){var t=e.next();return t.done?t:{value:r(t.value)}})}},{"./iterator.js":7}],9:[function(r,e,t){var n=r("./iterator.js");e.exports=function(r,e){var t=!1;if(!(r instanceof RegExp))throw new Error("obliterator/match: invalid pattern. Expecting a regular expression.");if("string"!=typeof e)throw new Error("obliterator/match: invalid target. Expecting a string.");return new n(function(){if(t&&!r.global)return r.lastIndex=0,{done:!0};t=!0;var n=r.exec(e);return n?{value:n}:(r.lastIndex=0,{done:!0})})}},{"./iterator.js":7}],10:[function(r,e,t){function n(r,e,t,n){for(var o=0;o<n;o++)r[o]=e[t[o]]}var o=r("./iterator.js");e.exports=function(r,e){if(!Array.isArray(r))throw new Error("obliterator/permutations: first argument should be an array.");var t=r.length;if(arguments.length<2&&(e=t),"number"!=typeof e)throw new Error("obliterator/permutations: second argument should be omitted or a number.");if(e>t)throw new Error("obliterator/permutations: the size of the subsequences should not exceed the length of the array.");var i,a=new Uint32Array(t),u=new Array(e),s=new Uint32Array(e),f=!0;for(i=0;i<t;i++)a[i]=i,i<e&&(s[i]=t-i);return i=e,new o(function o(){if(f)return f=!1,n(u,r,a,e),{value:u};var c,l;if(i--,i<0)return{done:!0};if(s[i]--,0===s[i]){for(c=a[i],l=i;l<t-1;l++)a[l]=a[l+1];return a[t-1]=c,s[i]=t-i,o()}return l=s[i],c=a[i],a[i]=a[t-l],a[t-l]=c,i=e,n(u,r,a,e),{value:u}})}},{"./iterator.js":7}],11:[function(r,e,t){var n=r("./iterator.js"),o=r("./combinations.js"),i=r("./chain.js");e.exports=function(r){var e=r.length,t=new Array(e+1);t[0]=n.of([]);for(var a=1;a<e+1;a++)t[a]=o(r,a);return i.apply(null,t)}},{"./chain.js":1,"./combinations.js":2,"./iterator.js":7}],12:[function(r,e,t){var n=r("./iterator.js");e.exports=function(r,e,t){1===arguments.length&&(e=r,r=0),arguments.length<3&&(t=1);var o=r,i=new n(function(){if(o<e){var r=o;return o+=t,{value:r}}return{done:!0}});return i.start=r,i.end=e,i.step=t,i}},{"./iterator.js":7}],13:[function(r,e,t){function n(r){var e="g";return r.multiline&&(e+="m"),r.ignoreCase&&(e+="i"),r.sticky&&(e+="y"),r.unicode&&(e+="u"),new RegExp(r.source,e)}var o=r("./iterator.js");e.exports=function(r,e){if(!(r instanceof RegExp))throw new Error("obliterator/split: invalid pattern. Expecting a regular expression.");if("string"!=typeof e)throw new Error("obliterator/split: invalid target. Expecting a string.");r=n(r);var t=!1,i=0;return new o(function(){if(t)return{done:!0};var n,o,a=r.exec(e);return a?(o=a.index+a[0].length,n=e.slice(i,a.index),i=o):(t=!0,n=e.slice(i)),{value:n}})}},{"./iterator.js":7}]},{},[6])(6)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment