Comparing version 0.1.0 to 0.2.0
@@ -1,2 +0,2 @@ | ||
!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.donot=t():n.donot=t()}(window,(function(){return function(n){var t={};function r(e){if(t[e])return t[e].exports;var o=t[e]={i:e,l:!1,exports:{}};return n[e].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=n,r.c=t,r.d=function(n,t,e){r.o(n,t)||Object.defineProperty(n,t,{enumerable:!0,get:e})},r.r=function(n){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})},r.t=function(n,t){if(1&t&&(n=r(n)),8&t)return n;if(4&t&&"object"==typeof n&&n&&n.__esModule)return n;var e=Object.create(null);if(r.r(e),Object.defineProperty(e,"default",{enumerable:!0,value:n}),2&t&&"string"!=typeof n)for(var o in n)r.d(e,o,function(t){return n[t]}.bind(null,o));return e},r.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return r.d(t,"a",t),t},r.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},r.p="",r(r.s=0)}([function(n,t,r){"use strict";function e(n){return document.querySelector(n)}function o(n){return Array.from(document.querySelectorAll(n))}function u(n,t,r,e,o,u,i){try{var f=n[u](i),c=f.value}catch(n){return void r(n)}f.done?t(c):Promise.resolve(c).then(e,o)}function i(n){return function(){var t=this,r=arguments;return new Promise((function(e,o){var i=n.apply(t,r);function f(n){u(i,e,o,f,c,"next",n)}function c(n){u(i,e,o,f,c,"throw",n)}f(void 0)}))}}function f(n){return c.apply(this,arguments)}function c(){return(c=i(regeneratorRuntime.mark((function n(t){var r,e;return regeneratorRuntime.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return n.next=2,window.fetch(t);case 2:return r=n.sent,n.next=5,r.json();case 5:return e=n.sent,n.abrupt("return",e);case 7:case"end":return n.stop()}}),n)})))).apply(this,arguments)}function a(n,t){var r;if("undefined"==typeof Symbol||null==n[Symbol.iterator]){if(Array.isArray(n)||(r=function(n,t){if(!n)return;if("string"==typeof n)return l(n,t);var r=Object.prototype.toString.call(n).slice(8,-1);"Object"===r&&n.constructor&&(r=n.constructor.name);if("Map"===r||"Set"===r)return Array.from(n);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return l(n,t)}(n))||t&&n&&"number"==typeof n.length){r&&(n=r);var e=0,o=function(){};return{s:o,n:function(){return e>=n.length?{done:!0}:{done:!1,value:n[e++]}},e:function(n){throw n},f:o}}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 u,i=!0,f=!1;return{s:function(){r=n[Symbol.iterator]()},n:function(){var n=r.next();return i=n.done,n},e:function(n){f=!0,u=n},f:function(){try{i||null==r.return||r.return()}finally{if(f)throw u}}}}function l(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function d(n){return n*(Math.PI/180)}function s(n,t){return n.filter(t)[0]}function p(n,t){var r,e=[],o=a(n);try{for(o.s();!(r=o.n()).done;){var u,i=r.value,f=a(t);try{for(f.s();!(u=f.n()).done;){var c=u.value;e.push([i,c])}}catch(n){f.e(n)}finally{f.f()}}}catch(n){o.e(n)}finally{o.f()}return e}function y(){return function(){}}function v(n){var t,r=[],e=new Set,o=a(p(n,n));try{for(o.s();!(t=o.n()).done;){var u=t.value;u[0]>u[1]&&(u=[u[1],u[0]]);var i=String(u[0])+String(u[1]);e.has(i)||(r.push(u),e.add(i))}}catch(n){o.e(n)}finally{o.f()}return r}function m(n){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return t||(t=n,n=0),Array.from(new Array(t-n),(function(t,r){return r+n}))}function h(n){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return t||(t=n,n=0),Math.round(Math.random()*(t-n))+n}function b(n){return g(n)[0]}function g(n){for(var t=n.length-1;t>0;t--){var r=Math.floor(Math.random()*(t+1)),e=[n[r],n[t]];n[t]=e[0],n[r]=e[1]}return n}function w(n){return n.reduce((function(n,t){return n+t}))}function S(n){return new Promise((function(t){window.setTimeout(t,n)}))}r.r(t),r.d(t,"$",(function(){return e})),r.d(t,"$$",(function(){return o})),r.d(t,"degToRad",(function(){return d})),r.d(t,"find",(function(){return s})),r.d(t,"getJson",(function(){return f})),r.d(t,"matrix",(function(){return p})),r.d(t,"noop",(function(){return y})),r.d(t,"permutations",(function(){return v})),r.d(t,"range",(function(){return m})),r.d(t,"randInt",(function(){return h})),r.d(t,"sample",(function(){return b})),r.d(t,"shuffle",(function(){return g})),r.d(t,"sum",(function(){return w})),r.d(t,"timeout",(function(){return S}))}])})); | ||
!function(n,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.donot=t():n.donot=t()}(this,(function(){return function(n){var t={};function r(e){if(t[e])return t[e].exports;var o=t[e]={i:e,l:!1,exports:{}};return n[e].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=n,r.c=t,r.d=function(n,t,e){r.o(n,t)||Object.defineProperty(n,t,{enumerable:!0,get:e})},r.r=function(n){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(n,"__esModule",{value:!0})},r.t=function(n,t){if(1&t&&(n=r(n)),8&t)return n;if(4&t&&"object"==typeof n&&n&&n.__esModule)return n;var e=Object.create(null);if(r.r(e),Object.defineProperty(e,"default",{enumerable:!0,value:n}),2&t&&"string"!=typeof n)for(var o in n)r.d(e,o,function(t){return n[t]}.bind(null,o));return e},r.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return r.d(t,"a",t),t},r.o=function(n,t){return Object.prototype.hasOwnProperty.call(n,t)},r.p="",r(r.s=0)}([function(n,t,r){"use strict";function e(n){return document.querySelector(n)}function o(n){return Array.from(document.querySelectorAll(n))}function u(n,t,r,e,o,u,i){try{var f=n[u](i),a=f.value}catch(n){return void r(n)}f.done?t(a):Promise.resolve(a).then(e,o)}function i(n){return function(){var t=this,r=arguments;return new Promise((function(e,o){var i=n.apply(t,r);function f(n){u(i,e,o,f,a,"next",n)}function a(n){u(i,e,o,f,a,"throw",n)}f(void 0)}))}}function f(n){return a.apply(this,arguments)}function a(){return(a=i(regeneratorRuntime.mark((function n(t){var r,e;return regeneratorRuntime.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return n.next=2,window.fetch(t);case 2:return r=n.sent,n.next=5,r.json();case 5:return e=n.sent,n.abrupt("return",e);case 7:case"end":return n.stop()}}),n)})))).apply(this,arguments)}function c(n,t){var r;if("undefined"==typeof Symbol||null==n[Symbol.iterator]){if(Array.isArray(n)||(r=function(n,t){if(!n)return;if("string"==typeof n)return l(n,t);var r=Object.prototype.toString.call(n).slice(8,-1);"Object"===r&&n.constructor&&(r=n.constructor.name);if("Map"===r||"Set"===r)return Array.from(n);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return l(n,t)}(n))||t&&n&&"number"==typeof n.length){r&&(n=r);var e=0,o=function(){};return{s:o,n:function(){return e>=n.length?{done:!0}:{done:!1,value:n[e++]}},e:function(n){throw n},f:o}}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 u,i=!0,f=!1;return{s:function(){r=n[Symbol.iterator]()},n:function(){var n=r.next();return i=n.done,n},e:function(n){f=!0,u=n},f:function(){try{i||null==r.return||r.return()}finally{if(f)throw u}}}}function l(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function d(n){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{allowEquals:!0},e=[],o=new Set,u=c(y(n,n));try{for(u.s();!(t=u.n()).done;){var i=t.value;if(!("allowEquals"in r)||r.allowEquals||i[0]!==i[1]){i[0]>i[1]&&(i=[i[1],i[0]]);var f=String(i[0])+String(i[1]);o.has(f)||(e.push(i),o.add(f))}}}catch(n){u.e(n)}finally{u.f()}return e}function s(n){return n*(Math.PI/180)}function y(n,t){var r,e=[],o=c(n);try{for(o.s();!(r=o.n()).done;){var u,i=r.value,f=c(t);try{for(f.s();!(u=f.n()).done;){var a=u.value;e.push([i,a])}}catch(n){f.e(n)}finally{f.f()}}}catch(n){o.e(n)}finally{o.f()}return e}function p(){return function(){}}function v(n){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return t||(t=n,n=0),Array.from(new Array(t-n),(function(t,r){return r+n}))}function m(n){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;return t||(t=n,n=0),Math.round(Math.random()*(t-n))+n}function h(n){return b(n)[0]}function b(n){for(var t=n.length-1;t>0;t--){var r=Math.floor(Math.random()*(t+1)),e=[n[r],n[t]];n[t]=e[0],n[r]=e[1]}return n}function g(n){return n.reduce((function(n,t){return n+t}))}function w(n){return new Promise((function(t){"undefined"==typeof window?setTimeout(t,n):window.setTimeout(t,n)}))}r.r(t),r.d(t,"$",(function(){return e})),r.d(t,"$$",(function(){return o})),r.d(t,"combinations",(function(){return d})),r.d(t,"degToRad",(function(){return s})),r.d(t,"getJson",(function(){return f})),r.d(t,"matrix",(function(){return y})),r.d(t,"noop",(function(){return p})),r.d(t,"range",(function(){return v})),r.d(t,"randInt",(function(){return m})),r.d(t,"sample",(function(){return h})),r.d(t,"shuffle",(function(){return b})),r.d(t,"sum",(function(){return g})),r.d(t,"timeout",(function(){return w}))}])})); | ||
//# sourceMappingURL=donot.js.map |
import { $, $$ } from './src/dom.js'; | ||
import { getJson } from './src/http.js'; | ||
import { | ||
degToRad, find, matrix, noop, permutations, range, randInt, sample, | ||
degToRad, find, matrix, noop, combinations, range, randInt, sample, | ||
shuffle, sum | ||
@@ -12,2 +12,3 @@ } from './src/math.js'; | ||
$$, | ||
combinations, | ||
degToRad, | ||
@@ -17,3 +18,2 @@ getJson, | ||
noop, | ||
permutations, | ||
range, | ||
@@ -20,0 +20,0 @@ randInt, |
{ | ||
"name": "donot", | ||
"version": "0.1.0", | ||
"description": "A utility library that you shouldn't use", | ||
"browser": "dist/donot.js", | ||
"main" : "index.js", | ||
"version": "0.2.0", | ||
"description": "Do not use this utility library", | ||
"main": "dist/donot.js", | ||
"scripts": { | ||
"dist": "webpack -p", | ||
"publish": "npm run dist && npm publish", | ||
"watch": "http-server & webpack -dw" | ||
@@ -40,2 +40,2 @@ }, | ||
} | ||
} | ||
} |
# donot | ||
> A Javascript utility library that you shouldn't use | ||
*Do not use this Javascript utility library* | ||
This is basically my collection of functions that i often use in projects. They can probably be found in other libraries (like Lodash), but i don't want to use those libraries (because they're big) or because it's overkill. | ||
This is basically my collection of functions that i often use in projects. They can probably be found in other libraries (like Lodash), but i might not want to use those libraries (because they're big) or because it's overkill. | ||
Because this is a bit of a grabbag of things you probably shouldn't be using this library. | ||
Because this is a bit of a grabbag of things you probably shouldn't be using this library. Hence the name. | ||
@@ -19,7 +19,93 @@ ## Install | ||
`donot` can be used as a CommonJS module, AMD (with Require.js) or as a plain old Javacript global. | ||
`donot` is a UMD library and thus can be used as a CommonJS module for Node.js, AMD (with Require.js) or as a plain old Javacript global. | ||
## Usage | ||
## API | ||
Here are all the functions: | ||
**`$(selector: string): HTMLElement`** | ||
A shortcut for `document.querySelector`. | ||
```javascript | ||
$("body").style.background = 'red';` | ||
```` | ||
**`$$(selector: string): HTMLElement`** | ||
A shortcut for `document.querySelectorAll`. Converts the elements in an array. | ||
```javascript | ||
$$("p").forEach((p) => p.style.color = 'red'); | ||
```` | ||
**`combinations(list: array, opts?:object): array`** | ||
Create a list of all unique combinations, where a,b === b,a. By default, equals like 'a,a' are allowed, if you don't want that, pass `{ allowEquals : false }` as a second argument. | ||
```javascript | ||
const options = combinations([1, 2]); | ||
console.log(options); // [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ] ] | ||
const options = combinations([1, 2], { allowEquals : false }); | ||
console.log(options); // [ [ 1, 2 ] ] | ||
```` | ||
**`degToRad(deg: number): number`** | ||
Converts from degrees to radians. | ||
```javascript | ||
console.log(degToRad(180)); // 3.141592.... | ||
```` | ||
**`getJson(url: string): Promise`** | ||
Uses `fetch` to do a HTTP call and return JSON. | ||
```javascript | ||
const data = await getJson('http://www.example.com/data.json'); | ||
```` | ||
**`matrix(cols: number, rows: number): arrray`** | ||
Creates a multidimensional array. | ||
```javascript | ||
const grid = matrix([1,2,3], [4,5,6]); | ||
console.log(grid[0][1]); // 4 | ||
```` | ||
**`noop(): function`** | ||
Returns an empty function. | ||
**`range(max: number): number`** | ||
**`range(min: number, max: number): number`** | ||
Creates a filled array with number from `min` (or 0 if that is not given) to, but not including `max`. | ||
```javascript | ||
console.log(range(3)); // [ 0, 1, 2 ] | ||
console.log(range(2, 5)); // [ 2, 3, 4 ] | ||
``` | ||
**`randInt(max: number) : number`** | ||
**`randInt(min: number, max: number)`** | ||
Returns a random integer between `min` (or 0 if that is not given) and `max`. | ||
**`sample(list: array): any`** | ||
Returns a random element from an array. | ||
**`shuffle(list: array): array`** | ||
Returns a shuffled (randomized) copy of `list`. | ||
**`sum(list: array): number`** | ||
Returns the sum of all numbers in `list`. | ||
```javascript | ||
console.log(sum([1,2,3,4])); // 10 | ||
```` | ||
**`timeout(ms: number): Promise`** | ||
Returns a promisified version of `window.setTimeout` (browser) or `setTimeout`) (Node) with a time of `ms` miliseconds. | ||
```javascript | ||
console.log("Let's wait 2 seconds"); | ||
await timeout(2000); | ||
console.log("That was 2 seconds"); | ||
```` | ||
## License | ||
MIT © [Hay Kranen](http://www.haykranen.nl) |
@@ -0,1 +1,28 @@ | ||
// Create a list of all unique combinations, where a,b === b,a | ||
export function combinations(list, opts = { allowEquals : true}) { | ||
const out = []; | ||
const keys = new Set(); | ||
for (let item of matrix(list, list)) { | ||
if ('allowEquals' in opts && !opts.allowEquals) { | ||
if (item[0] === item[1]) { | ||
continue; | ||
} | ||
} | ||
if (item[0] > item[1]) { | ||
item = [item[1], item[0]]; | ||
} | ||
const key = String(item[0]) + String(item[1]); | ||
if (!keys.has(key)) { | ||
out.push(item); | ||
keys.add(key); | ||
} | ||
} | ||
return out; | ||
} | ||
export function degToRad(deg) { | ||
@@ -21,23 +48,2 @@ return deg * (Math.PI / 180) | ||
// Create a list of all unique combinations, where a,b === b,a | ||
export function permutations(list) { | ||
const out = []; | ||
const keys = new Set(); | ||
for (let item of matrix(list, list)) { | ||
if (item[0] > item[1]) { | ||
item = [item[1], item[0]]; | ||
} | ||
const key = String(item[0]) + String(item[1]); | ||
if (!keys.has(key)) { | ||
out.push(item); | ||
keys.add(key); | ||
} | ||
} | ||
return out; | ||
} | ||
export function range(min, max = null) { | ||
@@ -44,0 +50,0 @@ if (!max) { |
export function timeout(ms) { | ||
return new Promise((resolve) => { | ||
window.setTimeout(resolve, ms); | ||
// Check if we're in a browser or not | ||
if (typeof window === 'undefined') { | ||
setTimeout(resolve, ms); | ||
} else { | ||
window.setTimeout(resolve, ms); | ||
} | ||
}); | ||
} |
@@ -10,2 +10,3 @@ const path = require('path'); | ||
libraryTarget : 'umd', | ||
globalObject : 'this', // Webpack 4 needs this for UMD to work in Node, see issue #6784 | ||
path: path.resolve(__dirname, 'dist'), | ||
@@ -12,0 +13,0 @@ }, |
Sorry, the diff of this file is not supported yet
23542
13
155
110