Skip to content

Instantly share code, notes, and snippets.

View pipe.js
pipe = (...fns) => (initialVal) => fns.reduce((val, fn) => fn(val), initialVal);
const getDiscountedPrice = pipe(applyFixDiscount, applyVipOffer);
View compose.js
compose =
(...fns) =>
(initialVal) =>
fns.reduceRight((val, fn) => fn(val), initialVal);
const getDiscountedPrice = compose(applyVipOffer, applyFixDiscount);
console.log('price:', getDiscountedPrice2(100)) // 40
View simple composition.js
const applyFixDiscount= (x) => x - 20
const applyVipOffer = (x) => x / 2
const getDiscountedPrice = (x) => applyVipOffer(applyFixDiscount(x))
console.log(getDiscountedPrice(100)) // 40
View promises with reduce.js
const allPromises = [fetchMockData('John', 4000), fetchMockData('Peter')];
allPromises.reduce(async (p, curr) => {
await p;
return curr.then((result) => {
console.log('result:', result);
return curr;
});
}, Promise.resolve());
View promise all.js
// simulate async operation
function fetchMockData(name, timeToWait = 2000) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ name: name });
}, timeToWait);
});
}
const allPromises = [fetchMockData('John'), fetchMockData('Peter')];
Promise.all(allPromises)
View vue3 setter.ts
const hadKey =
isArray(target) && isIntegerKey(key)
? Number(key) < target.length
: hasOwn(target, key)
const result = Reflect.set(target, key, value, receiver)
// don't trigger if target is something up in the prototype chain of original
if (target === toRaw(receiver)) {
if (!hadKey) {
trigger(target, TriggerOpTypes.ADD, key, value)
} else if (hasChanged(value, oldValue)) {
View vue 3 reactivity.ts
function createReactiveObject(
target: Target,
isReadonly: boolean,
baseHandlers: ProxyHandler<any>,
collectionHandlers: ProxyHandler<any>,
proxyMap: WeakMap<Target, any>
) {
if (!isObject(target)) {
if (__DEV__) {
console.warn(`value cannot be made reactive: ${String(target)}`)
View vue 2 reactivity.js
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter () {
const value = getter ? getter.call(obj) : val
if (Dep.target) {
dep.depend()
if (childOb) {
childOb.dep.depend()
if (Array.isArray(value)) {
View openwhisk serverless.yml
service: nodewhiskservice
provider:
name: openwhisk
ignore_certs: true
functions:
hello:
handler: handler.hello
events:
View lazyloadingRoute.ts
{
path: 'home',
data: { name: 'home', title: 'Home', modules: ['default'] },
loadChildren: () =>
import('./patient/patient.module').then((m) => m.PatientModule),
}