Add pick to frptools. Document all utility functions

This commit is contained in:
Timothy Farrell 2017-12-31 04:50:24 -06:00
parent b1f4bb0e52
commit 0a2aff55f7
6 changed files with 36 additions and 8 deletions

View File

@ -228,3 +228,28 @@ node to use to propagate updates but firing other values could have some uses.
const unsubscribe = inViewport.subscribe(console.log.bind(console)); const unsubscribe = inViewport.subscribe(console.log.bind(console));
inViewport.fire(false); // "false" logged to console. inViewport.fire(false); // "false" logged to console.
``` ```
# Utilities
## id
`id(anything) -> anything`
`id` is a function that returns its first parameter. It is used as the default hash function for
each subscribable.
## call
`call(anything) -> void`
`call` will call the first parameter if it is a function. It is used for iterating over
subscribables.
## pick
`pick(propName, default) -> (obj) -> any`
`pick` returns a function that accepts an object and will return the object's property for the
provided name or the default if one is supplied. `pick` is not directly used with any subscribable
but can be useful as the computed function when breaking down a `prop` that contains an object or a
container.

View File

@ -1,6 +1,6 @@
{ {
"name": "frptools", "name": "frptools",
"version": "3.0.0", "version": "3.0.1",
"description": "Observable Property and Computed data streams", "description": "Observable Property and Computed data streams",
"main": "lib/index.js", "main": "lib/index.js",
"jsnext:main": "src/index.js", "jsnext:main": "src/index.js",

View File

@ -1,4 +1,4 @@
export { prop } from './property'; export { prop } from './property';
export { computed } from './computed'; export { computed } from './computed';
export { container } from './container'; export { container } from './container';
export { call, id } from './util.js'; export { call, id, pick } from './util.js';

View File

@ -1,5 +1,11 @@
export const id = a => a; export const id = a => a;
export const pick = (id, def) => doc => (doc && doc.hasOwnProperty(id) ? doc[id] : def);
export const call = a => (typeof a === 'function' ? a() : a);
// internal utilities
export const registerSubscriptions = subscriptionsArray => fn => { export const registerSubscriptions = subscriptionsArray => fn => {
subscriptionsArray.push(fn); subscriptionsArray.push(fn);
return () => { return () => {
@ -11,8 +17,6 @@ export const registerSubscriptions = subscriptionsArray => fn => {
}; };
}; };
export const call = a => (typeof a === 'function' ? a() : a);
export const registerFire = subscriptionsArray => val => { export const registerFire = subscriptionsArray => val => {
subscriptionsArray.map(s => s(val)).forEach(call); subscriptionsArray.map(s => s(val)).forEach(call);
}; };

View File

@ -1,8 +1,7 @@
import { prop, computed, container } from 'frptools'; import { prop, computed, container, pick } from 'frptools';
import { Icon } from './icon.js'; import { Icon } from './icon.js';
import { defineElement as el, subscribeToRender } from '../../utils/domvm.js'; import { defineElement as el, subscribeToRender } from '../../utils/domvm.js';
import { pick } from '../../utils/conversion.js';
import { injectStyle, styled } from '../../services/style.js'; import { injectStyle, styled } from '../../services/style.js';
import { CLICKABLE } from '../styles.js'; import { CLICKABLE } from '../styles.js';

View File

@ -1,4 +1,6 @@
import { readAsArrayBuffer } from 'pouchdb-binary-utils'; import { readAsArrayBuffer } from 'pouchdb-binary-utils';
import { pick } from 'frptools';
import { isObject } from './comparators'; import { isObject } from './comparators';
export function bufferToHexString(buffer) { export function bufferToHexString(buffer) {
@ -45,8 +47,6 @@ export function deepAssign(to, ...rest) {
return to; return to;
} }
export const pick = (id, def) => doc => (doc && doc.hasOwnProperty(id) ? doc[id] : def);
export const extractID = pick('_id'); export const extractID = pick('_id');
export const extractREV = pick('_rev'); export const extractREV = pick('_rev');