Skip to content

Instantly share code, notes, and snippets.

View ourmaninamsterdam's full-sized avatar

Justin Perry ourmaninamsterdam

View GitHub Profile
@ourmaninamsterdam
ourmaninamsterdam / hyphenateText.js
Last active November 2, 2020 13:51
Forced text hyphenation
const hyphenateText = (text, breakpoint) => {
if (text.length > breakpoint) {
const words = text.split(' ');
return words.map((word) => {
if (word.length > breakpoint) {
const head = word.substr(0, breakpoint);
const tail = word.substr(breakpoint);
return `${head} -${tail}`;
}
return word;
@ourmaninamsterdam
ourmaninamsterdam / enumerators.js
Created November 1, 2019 14:57
As Enumerables
const asEnumeration = dictionary => Object.freeze({
from(value) {
if (dictionary[value]) {
return dictionary[value];
}
throw Error(`Invalid enumeration value ${value}`);
}
});
const getEvenNumbers = limit => includeIfConditionIsMet(limit, number => number % 2 === 0);
@ourmaninamsterdam
ourmaninamsterdam / generateIncrementedNumbersList.js
Last active November 1, 2019 14:55
Generate incremented numbers
const generateIncrementedNumbersList = (min, max, increment, includeMin) => {
const items = [];
let incrementor = Math.max(min, increment);
if (includeMin) {
items.push(min);
}
while (incrementor <= max) {
items.push(incrementor);
const add = (a, b) => a + b;
const add10 = num => add(num, 10);
const add200 = num => add(num, 200);
const addNew = a => b => a + b;
console.log(add(10, 20)) // 30
console.log(add10(10)) // 20
console.log(addNew(10)(20)) // 30
console.log(add200(10)) // 210
@ourmaninamsterdam
ourmaninamsterdam / rxRangeSlider.js
Last active October 17, 2019 20:31
RxRangeSlider - first play with RxJS
const rxRangeSlider = ({ rangeNode, handlesContainerNode, handleStartNode, handleEndNode, start: [stepStart, stepEnd], stepSize, bounds: [rangeStart, rangeEnd] }) => {
const KEY_LEFT = 37;
const KEY_RIGHT = 39;
const VALID_KEYS = [KEY_LEFT, KEY_RIGHT];
const INCREMENT_LOWER = 'INCREMENT_LOWER';
const DECREMENT_LOWER = 'DECREMENT_LOWER';
const INCREMENT_UPPER = 'INCREMENT_UPPER';
const DECREMENT_UPPER = 'DECREMENT_UPPER';
const restrictToRange = (min, max, num) => Math.min(Math.max(min, num), max);
@ourmaninamsterdam
ourmaninamsterdam / omit.js
Created October 16, 2019 14:50
Omit object property
const omit = (object, key) => {
const objectCopy = Object.assign({}, object);
Object.keys(objectCopy).forEach(itemKey => {
if(key === itemKey) {
delete objectCopy[itemKey];
}
})
return objectCopy;
};
@ourmaninamsterdam
ourmaninamsterdam / rxArrayStream.js
Last active October 16, 2019 14:08
RxJS quick example of switching between array and stream
Rx.Observable.range(1, 10)
.toArray()
.mergeMap(items => Rx.Observable.from(items))
.map(a => a + a)
.subscribe({
next(items) {
console.log(items);
}
@ourmaninamsterdam
ourmaninamsterdam / mapKeys.js
Created October 14, 2019 17:33
Maps array of objets to a key value object using key provided
const mapKeys = (collection, rootKey) => {
const obj = {};
collection.forEach(item => {
obj[item[rootKey]] = item;
});
return obj;
};
const justins = [{id: 1, name: 'Justin Bieber'},{id: 2, name: 'Justin Timberlake'},{id: 3, name: 'Justin Time'}];
@ourmaninamsterdam
ourmaninamsterdam / gist:d5cb3d2ad91d1022c01cfe05c43db914
Last active October 11, 2019 13:10
Split string, flatten and convert ranges to array of numbers
"1,40-44"
.split(',')
.map(item => item.trim().split('-')
.map(item => parseInt(item.trim()))) // [[1], [40,44]]
"2-3,10,12,45,46-59"
.split(',')
.map(item => item.trim().split('-')
.map(item => parseInt(item.trim()))) // [[2,3], [10,12,45], [46,59]]