Skip to content

Instantly share code, notes, and snippets.

@tkh44
Last active September 13, 2022 01:33
Show Gist options
  • Save tkh44/3475eeb1915c5185c6ddcafb84b3207c to your computer and use it in GitHub Desktop.
Save tkh44/3475eeb1915c5185c6ddcafb84b3207c to your computer and use it in GitHub Desktop.
utility functions
import isPlainObject from 'is-plain-object';
export const keys = Object.keys;
export const assign = Object.assign;
export const merge = (...args) => assign({}, ...args);
export const reduceObj = (cb, obj = {}) => {
return keys(obj).reduce(cb, {});
};
export const mapObj = (cb, obj = {}) => {
return keys(obj).map((key) => {
return cb(obj[key], key);
});
};
export const mapValues = (cb, obj = {}) => {
return reduceObj((accum, key) => {
accum[key] = cb(obj[key], key, obj);
return accum;
}, obj);
};
export const pick = (targets, obj = {}) => {
return reduceObj((accum, key) => {
if (targets.indexOf(key) !== -1) {
accum[key] = obj[key];
}
return accum;
}, obj);
};
export const omit = (omissions, obj = {}) => {
if (!Array.isArray(omissions)) {
omissions = [omissions];
}
return reduceObj((accum, key) => {
if (omissions.indexOf(key) === -1) {
accum[key] = obj[key];
}
return accum;
}, obj);
};
export const last = (arr) => arr[arr.length - 1];
export const clamp = (n, min, max) => (Math.max(Math.min(n, max), min));
export const isString = (thing) => (thing && typeof thing === 'string');
export const isFunction = (thing) => (thing && typeof thing === 'function');
export const isBool = (thing) => typeof thing === 'boolean';
export const isObject = isPlainObject;
export const noop = (...args) => {
if (__DEVELOPMENT__ && args) {
console.log(...args.filter(Boolean));
}
};
export const logWithLabel = (content, label, collapsed = false) => {
if (__DEVELOPMENT__) {
content = Array.isArray(content) ? content : [content];
console[collapsed ? 'groupCollapsed' : 'group'](label.toUpperCase());
content.forEach((c) => {
if (Array.isArray(c) && Object.prototype.toString.call(c[0]) === '[object Object]') {
return console.table(c, Object.keys(c[0]));
}
if (Object.prototype.toString.call(c) === '[object Object]') {
return console.dir(c);
}
let message = c;
if (typeof c === 'string') {
message = `"${c}"`;
}
console.log(message);
});
console.groupEnd();
}
};
export const animationFrameThrottle = function (cb) {
let isQueued;
return (...args) => {
if (!isQueued) {
isQueued = true;
window.requestAnimationFrame(() => {
cb.apply(this, args);
isQueued = false;
});
}
};
};
// tl;dr ...set the duration of a spring animation
export const configw = (sec, overdamping = 0) => {
const s = overdamping <= 0
? 1 - overdamping
: 1 / Math.sqrt(1 + Math.pow(2 * Math.PI / Math.log(1 / (overdamping * overdamping)), 2));
const ks = (2 * Math.PI / sec) / Math.max(Math.sqrt(1 - s * s), 0.5);
const c = 2 * ks * s;
return [ks * ks, c];
};
import { expect } from 'code';
import {
merge,
pick,
omit,
mapValues,
mapObj,
last
} from 'utils/func';
describe('Utility Functions', () => {
describe('merge', () => {
it('should behave like object assign', () => {
const a = {a: 1, b: 2};
const b = {c: 3, d: 4};
expect(merge(a,b)).to.deep.equal(Object.assign({}, a, b));
});
});
describe('omit', () => {
it('omits keys', () => {
const a = {a: 1, b: 2};
expect(omit(['a'], a)).to.deep.equal({ b: 2 });
});
});
describe('pick', () => {
it('omits keys', () => {
const a = {a: 1, b: 2};
expect(pick(['a'], a)).to.deep.equal({ a: 1 });
});
});
describe('mapValues', () => {
it('maps over values and keeps keys', () => {
const a = {a: 1, b: 2};
const result = mapValues((val) => val * 2, a);
expect(result).to.deep.equal({a: 2, b: 4});
});
});
describe('mapObj', () => {
it('maps over object and returns array result of mapped values', () => {
const a = {a: 1, b: 2};
const result = mapObj((val) => val * 2, a);
expect(result).to.deep.equal([2, 4]);
});
});
describe('last', () => {
it('returns last item in array', () => {
expect(last([2, 4])).to.equal(4);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment