added query string parser and functional tools
This commit is contained in:
267
src/funky.js
Normal file
267
src/funky.js
Normal file
@@ -0,0 +1,267 @@
|
||||
export function chain (obj) {
|
||||
let chainInstance = {
|
||||
value: function () {
|
||||
return obj;
|
||||
}
|
||||
};
|
||||
|
||||
forEach(_, function (method, key) {
|
||||
chainInstance[key] = function () {
|
||||
obj = method(obj, ...arguments);
|
||||
return chainInstance;
|
||||
};
|
||||
});
|
||||
|
||||
return chainInstance;
|
||||
}
|
||||
|
||||
export function compact (obj) {
|
||||
return filter(obj, val => val ? true : false);
|
||||
}
|
||||
|
||||
export function contains (obj, value) {
|
||||
if (Array.isArray(obj)) {
|
||||
let length = obj.length;
|
||||
for (let i=0; i<length; i++) {
|
||||
if (obj[i] === value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (let key in obj) {
|
||||
if (obj[key] === value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function filter (obj, predicate) {
|
||||
let result = {};
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.filter(predicate);
|
||||
}
|
||||
else {
|
||||
forEach(obj, (val, key) => {if (predicate(val, key)) result[key] = val;});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function find (obj, predicate) {
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.find(predicate);
|
||||
}
|
||||
else {
|
||||
for (let key in obj) {
|
||||
if (predicate(obj[key])) return obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function forEach (obj, iteratee) {
|
||||
if (Array.isArray(obj)) {
|
||||
// native 'forEach' but the return below allows us to chain
|
||||
obj.forEach(iteratee);
|
||||
}
|
||||
else {
|
||||
for (let key in obj) {
|
||||
iteratee(obj[key], key, obj);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
export function groupBy (obj, iteratee) {
|
||||
let result = {};
|
||||
|
||||
|
||||
forEach(obj, val => {
|
||||
let key = typeof iteratee === 'string' ? val[iteratee] : iteratee(val);
|
||||
|
||||
result[key] = result[key] || [];
|
||||
result[key].push(val);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function identity (value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
export function map (obj, iteratee) {
|
||||
let result = [];
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
// native 'map' but the return below allows us to chain
|
||||
return obj.map(iteratee);
|
||||
}
|
||||
else {
|
||||
forEach(obj, (value, key) => {
|
||||
result.push(iteratee(value, key, obj));
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function isObject (obj) {
|
||||
return obj && (typeof obj === 'object') && !Array.isArray(obj) ? true : false;
|
||||
}
|
||||
|
||||
function has (obj, prop) {
|
||||
return obj && obj.hasOwnProperty(prop);
|
||||
}
|
||||
|
||||
export function mapObj (obj, iteratee) {
|
||||
let result = {};
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
forEach(obj, (value, index) => {
|
||||
let returned = iteratee(value, index, obj);
|
||||
|
||||
if (isObject(returned) && has(returned, 'key') && has(returned, 'value')) {
|
||||
result[returned.key] = returned.value;
|
||||
}
|
||||
else {
|
||||
result[index] = returned;
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
forEach(obj, (value, key) => {
|
||||
let returned = iteratee(value, key, obj);
|
||||
|
||||
if (isObject(returned) && has(returned, 'key') && has(returned, 'value')) {
|
||||
result[returned.key] = returned.value;
|
||||
}
|
||||
else {
|
||||
result[key] = returned;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function max (obj = [], iteratee) {
|
||||
let max = -Infinity;
|
||||
|
||||
if (!iteratee) {
|
||||
forEach(obj, value => max = value > max ? value : max);
|
||||
}
|
||||
else {
|
||||
let lastComputed = -Infinity;
|
||||
|
||||
forEach(obj, (value, index, list) => {
|
||||
let computed = iteratee(value, index, list);
|
||||
|
||||
if (computed > lastComputed || computed === -Infinity && max === -Infinity) {
|
||||
max = value;
|
||||
lastComputed = computed;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
export function min (obj = [], iteratee) {
|
||||
let min = Infinity;
|
||||
|
||||
if (!iteratee) {
|
||||
forEach(obj, value => min = value < min ? value : min);
|
||||
}
|
||||
else {
|
||||
let lastComputed = Infinity;
|
||||
|
||||
forEach(obj, (value, index, list) => {
|
||||
let computed = iteratee(value, index, list);
|
||||
|
||||
if (computed < lastComputed || computed === Infinity && min === Infinity) {
|
||||
min = value;
|
||||
lastComputed = computed;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return min;
|
||||
}
|
||||
|
||||
export function omit (obj, keys) {
|
||||
let result = {};
|
||||
|
||||
forEach(obj, (value, key) => {
|
||||
if (!contains(keys, key)) {
|
||||
result[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function pick (obj, keys) {
|
||||
let result = {};
|
||||
|
||||
keys.forEach(key => {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
result[key] = obj[key];
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function range (size, iteratee = identity) {
|
||||
|
||||
return Array.from(Array(size), (value, index) => iteratee(index));
|
||||
}
|
||||
|
||||
export function reduce (obj = [], iteratee, memo = obj[0] != null ? obj[0] : 0) {
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.reduce(iteratee, memo);
|
||||
}
|
||||
else {
|
||||
forEach(obj, (val, key, obj) => {
|
||||
memo = iteratee(memo, val, key, obj);
|
||||
});
|
||||
}
|
||||
|
||||
return memo;
|
||||
}
|
||||
|
||||
export function reject (obj, predicate) {
|
||||
return filter(obj, (val, key, obj) => !predicate(val, key, obj));
|
||||
}
|
||||
|
||||
export function some (obj, predicate = identity) {
|
||||
return find(obj, predicate) ? true : false;
|
||||
}
|
||||
|
||||
export let _ = {
|
||||
compact,
|
||||
contains,
|
||||
filter,
|
||||
find,
|
||||
forEach,
|
||||
groupBy,
|
||||
identity,
|
||||
map,
|
||||
mapObj,
|
||||
max,
|
||||
min,
|
||||
omit,
|
||||
pick,
|
||||
range,
|
||||
reduce,
|
||||
reject,
|
||||
some
|
||||
};
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
import Tessellate from './tessellate.js';
|
||||
|
||||
import {utils} from './tessellate.js';
|
||||
@@ -11,14 +10,16 @@ class Demo {
|
||||
|
||||
this.map = [];
|
||||
|
||||
let tessellate = new Tessellate({
|
||||
const queryStringObj = utils.getQueryStringObj();
|
||||
|
||||
let tessellate = new Tessellate(Object.assign({
|
||||
element: '#container',
|
||||
board: Tessellate.BOARD_STYLES.HEX,
|
||||
tile: Tessellate.TILE_STYLES.HEX,
|
||||
tap: this.onTap,
|
||||
draw: this.draw,
|
||||
pointyTop: false, //utils.random(1) ? true : false,
|
||||
});
|
||||
}, queryStringObj));
|
||||
|
||||
this.circle = new DrawCircle();
|
||||
this.square = new DrawSquare();
|
||||
|
||||
34
src/utils.js
34
src/utils.js
@@ -1,3 +1,4 @@
|
||||
import {mapObj} from './funky.js';
|
||||
|
||||
export function noop() {};
|
||||
|
||||
@@ -82,3 +83,36 @@ export function rangeInclusive(start, end) {
|
||||
return range(Math.min(start, end), Math.max(start, end)+1);
|
||||
}
|
||||
|
||||
export function getQueryStringParameters (queryString = document.location.search, separator = '&', assignment = '=') {
|
||||
let parameters = {};
|
||||
|
||||
queryString
|
||||
.replace(/(^\?)/,'')
|
||||
.split(separator)
|
||||
.forEach(pair => {
|
||||
pair = pair.split(assignment);
|
||||
parameters[pair[0]] = pair[1];
|
||||
});
|
||||
|
||||
return parameters;
|
||||
}
|
||||
|
||||
export function getQueryStringObj (queryString = document.location.search, separator = '&', assignment = '=') {
|
||||
return mapObj(getQueryStringParameters(queryString, separator, assignment), parseString);
|
||||
}
|
||||
|
||||
const braced = /^{.*}$/;
|
||||
const bracketed = /^\[.*\]$/;
|
||||
|
||||
export function parseString (str) {
|
||||
return typeof str !== 'string' ? str :
|
||||
str.toLowerCase() === 'true' ? true :
|
||||
str.toLowerCase() === 'false' ? false :
|
||||
str.toLowerCase() === 'null' ? null :
|
||||
str.toLowerCase() === 'undefined' ? undefined :
|
||||
!isNaN(Number(str)) ? Number(str) :
|
||||
braced.test(str) || bracketed.test(str) ? JSON.parse(decodeURIComponent(str)) :
|
||||
str;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user