Skip to content

Instantly share code, notes, and snippets.

View rolandpeelen's full-sized avatar

Roland Peelen rolandpeelen

View GitHub Profile
_this.requestTimeOutStatus = function (row, seat) {
var currentStatus = SeatService.getStatusForSeatWith(row, seat);
var fireBaseTimeDifference = reservationTime - moment().diff(moment(JSON.parse(currentStatus.date)), 'seconds');
return fireBaseTimeDifference > 60 ? Math.round(fireBaseTimeDifference / 60) + 'm' : Math.round(fireBaseTimeDifference) + 's';
};
@rolandpeelen
rolandpeelen / version2.js
Last active July 22, 2018 18:48
A way to change a millisecond string to either 'Xh' or 'Xm' or 'Xs'
transform(value: any, args?: any): any {
if (typeof value !== 'number') return '…'
const inHours = value / 60 / 60 / 1000
const inMinutes = value / 60 / 1000
const inSeconds = value / 1000
if (value < 1) return '…'
if (inHours > 1) return `${Math.round(inHours)}h`
if (inMinutes > 1) return `${Math.round(inMinutes)}m`
return `${Math.round(inSeconds)}s`
}
@rolandpeelen
rolandpeelen / styleTime.js
Last active July 22, 2018 19:16
A more functional approach
const styleTime = ({completed, total}) => loadingStringIf(completed) || msAsHoursRounded(total) || msAsMinutesRounded(total) || msAsSecondsRounded(total) || loadingString();
@rolandpeelen
rolandpeelen / loadingStringIfCompleted.js
Created July 22, 2018 18:50
if value is true return function
import {curry} from "ramda";
// Take a function and a value, return the function with the applied value if the value is evaluated as true
const falseOr = curry((fn, value) => value && fn(value));
// Return a string that indicates loading
const loadingString = () => '…';
// Return the loading string but only if the input evaluates to true
const loadingStringIf = falseOr(loadingString);
@rolandpeelen
rolandpeelen / asHours.js
Created July 22, 2018 18:51
Function composition that takes some ms, converts it to hours, check if there are more than one, rounds it and postfixes it with an 'h'
import {compose, curry} from "ramda";
// Take a function and a value, return the function with the applied value if the value is evaluated as true
const falseOr = curry((fn, value) => value && fn(value));
// Take a function and a value, return the value if the function returns true
const valueIf = curry((fn, value) => fn(value) && value);
// Take a duration in ms and convert it to either Hours, minutes or seconds;
const msToHours = (duration) => duration / 60 / 60 / 1000;
// Check if a value evaluates to true and if it is over one
const overOne = (value) => isNumber(value) && value > 1;
// Postfix function that takes a postfix and a value and adds it to the back of the value
@rolandpeelen
rolandpeelen / anotherWayToWriteThis.js
Created July 22, 2018 18:53
Some different Syntaxes
const asHours = (duration) => {
 const stepOne = msToHours(duration);
 const stepTwo = valueIf(overOne(stepOne));
 const stepThree = falseOr(Math.round(stepTwo));
 const stepFour = falseOr(PostFixHours(stepThree));
 return stepFour;
}
// Or, if you're a mad man. Like this:
const asHours = (duration) => falseOr(postFixHours(falseOr(Math.round(valueIf(overOne(msToHours(duration)))))));
@rolandpeelen
rolandpeelen / msToHours.js
Created July 22, 2018 18:54
take milliseconds and turn them into hours
// Take a duration in ms and convert it to either Hours, minutes or seconds;
const msToHours = (duration) => duration / 60 / 60 / 1000;
@rolandpeelen
rolandpeelen / valueIf(overOne).js
Last active July 22, 2018 19:57
Check if a value is overOne and if so, return the value
import {compose, curry} from "ramda";
// Check if a value is number
const isNumber = (value) => typeof value === 'number';
// Take a function and a value, return the value if the function returns true
const valueIf = curry((fn, value) => fn(value) && value);
// Check if a value evaluates to true and if it is over one
const overOne = (value) => isNumber(value) && value > 1;
const asHours = (duration) => {
 const stepOne = msToHours(duration);
 const stepTwo = valueIf(overOne, stepOne);
 const stepThree = falseOr(Math.round(stepTwo));
 const stepFour = falseOr(PostFixHours(stepThree));
 return stepFour;
}
const asHours = compose(falseOr(postFixHours), falseOr(Math.round), valueIf(overOne, stepOne?), msToHours);