Skip to content

Instantly share code, notes, and snippets.

View crshmk's full-sized avatar

Chris Hammock crshmk

  • Nha Trang, Vietnam
  • 18:43 (UTC +07:00)
View GitHub Profile
@crshmk
crshmk / obs.service.ts
Last active November 21, 2021 00:31
ngModel updating shared service state with BehaviorSubject
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
export class ObsService {
private val$:BehaviorSubject<number> = new BehaviorSubject(42);
val = this.val$.asObservable();
}
@crshmk
crshmk / filterMany.js
Last active August 12, 2018 01:26
Apply array of filters
/**
* @param array of unary functions
* @param array of any
* @return array of any
*/
filterMany = (f, d) =>
f.length < 2 ? d.filter(f[0]) : filterMany(f, d.filter(f.shift()))
@crshmk
crshmk / empty.js
Created February 18, 2018 06:51
Empty test
/**
* empty array
* empty string
* function of zero arity
* empty object
* null
* undefined
*/
var empty = x => Array.isArray(x) || typeof x === 'string' || typeof x === 'function' ?
!x.length : null == x || Object.keys(x).length === 0 && x.constructor === Object
@crshmk
crshmk / Chain.js
Last active January 27, 2019 01:48
A simple chaining strategy
let methods = {
upp: function() {
return Chain(this.x.toUpperCase());
},
slice: function(len) {
return Chain(this.x.slice(len));
},
val: function() {
return this.x
}
@crshmk
crshmk / disjunction.js
Created March 4, 2018 14:54
Functor chaining with null handler
/**
* Test for null when chaining functor wrappers
* If not null, call chained functions on passed value
* If null occurs, disregard the function and pass along null
* When folding value, provide error and success handlers
*/
// functor setup
const Call = x =>
@crshmk
crshmk / pipe.js
Created March 5, 2018 15:04
A simple pipeline strategy
/**
* create a pipeline of unary functions
* examples at https://github.com/crshmk/pipe/blob/master/index.js
*/
let pipe = function() {
var fs = Array.prototype.slice.call(arguments);
function caller(fs, acc) {
return fs.length === 0 ? acc
: caller(fs.slice(1), fs[0](acc));
};
@crshmk
crshmk / flatten.js
Last active January 26, 2019 17:29
Flatten array
/**
* @param xs array
* @param oneLevel boolean -> pass true to flatten only one level
*/
let flatten = (xs, oneLevel) =>
oneLevel ? xs.reduce( (acc, xs) => acc.concat(xs), []) :
xs.reduce( (acc, x) => Array.isArray(x) ? acc.concat(flatten(x)) : acc.concat(x), []);
let nums = [1, [2, [3, 4, [5] ]]]
@crshmk
crshmk / test.spec.js
Last active October 7, 2018 05:05
vue-test-utils mounting functions
import { m, sm, mountWithPlugin } from './utils.js'
// reduce mounting boilerplate
import Component from '@/views/Comp.vue'
const wrapper = m(Component)
@crshmk
crshmk / update_node.sh
Created November 11, 2018 18:13
update node
# https://github.com/creationix/nvm
nvm install node --reinstall-packages-from=node
nvm ls
nvm alias default ${newVersion}
nvm uninstall ${oldVersion}
@crshmk
crshmk / async.js
Last active November 21, 2018 19:13
redux async structures
function fetcher(url) {
return fetch(url).then(res => res.json())
}
async function get(url) {
var result = await fetcher(url);
return result
}
get('https://jsonplaceholder.typicode.com/users')