1.3.0 • Published 8 years ago

pick-alternate-value v1.3.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

pick-alternate-value

NPM version Build Status Coveralls Status Dependency Status Downloads

Pick the most suitable value from a list of possible values

This is a flexible library which facilitates the rendering of text based on a template and a list of alternate parameters. Alternate parameters can be evaluated against a criteria to select the fittest.

Install

npm i -D pick-alternate-value

Usage

import pav from "pick-alternate-value"

const tUpper = value => value.toUpperCase();
const conf = {
  templates: ['<a>{{a}}</a> to <b>{{b}}</b> to <c>{{c}}</c>',
    '<c>{{c}}</c>'],
  props: { a: ['k1', 'k2'], b: [tUpper, 'k3'], c: ['k4', 'k5', 'k6'] },
  placeholders: {
    clean: [['<a>{{', '}}</a>']],
    extract: [['<b>{{', '}}</b>'], ['<c>{{', '}}</c>']],
  },
};

const data = {
  k1: 'k1v1',
  k2: 'k1v11',
  k3: 'k3v',
  k4: 'k4v',
  k6: 'k4v66',
};
pav.renderLongest(conf, data)
// return <a>k1v1</a> to <b>K3V</b> to <c>k4v66</c>
pav.renderLongest(conf, data, 10)
// return <c>k4v</c>

Functions

pickLongestSize(list, defaultValue, max) ⇒ object

Pick the first string (or object) with the longest size

Kind: global function
Returns: object - The object or string that is the longest

ParamTypeDescription
listarraylist of strings or objects
defaultValueobjectthe object/string to return if null
maxintegerthe maximum size that is allowed

Example

// returns abcd
pav.pickLongestSize(['ab', 'a', 'abcd'])

pickShortestSize(list, defaultValue, min) ⇒ object

Pick the first string (or object) with the shortest size

Kind: global function
Returns: object - The object or string that is the longest

ParamTypeDescription
listarraylist of strings or objects
defaultValueobjectthe object/string to return if null
minintegerthe minimum size that is allowed

Example

// returns a1
pav.pickShortestSize(['abd', 'a1', 'a2', 'abc'])

sumSize(list) ⇒ integer

Adds the size of all the items in the list

Kind: global function
Returns: integer - The sum of all the sizes

ParamTypeDescription
listarraylist of strings or objects

Example

// returns 7
pav.sumSize(['ab', 'a', 'abcd'])

minSize(list) ⇒ integer

Finds the minimum size of all the items in the list

Kind: global function
Returns: integer - The minimum of all the sizes

ParamTypeDescription
listarraylist of strings or objects

Example

// returns 1
pav.minSize(['ab', 'a', 'abcd'])

maxSize(list) ⇒ integer

Finds the maximum size of all the items in the list

Kind: global function
Returns: integer - The maximum of all the sizes

ParamTypeDescription
listarraylist of strings or objects

Example

// returns 4
pav.maxSize(['ab', 'a', 'abcd'])

hasNoNull(list) ⇒ integer

Returns true if the list does not contain any null

Kind: global function
Returns: integer - true if no null

ParamTypeDescription
listarraylist of strings or objects

Example

// returns false
pav.hasNoNull(['ab', null, 'abcd'])

discardPlaceholders(template, phStart, phEnd) ⇒ string

Discard the placeholders of a string template. Useful to check the minimum length of a template.

Kind: global function
Returns: string - The template without ny placeholders

ParamTypeDescription
templatestringlist of strings or objects
phStartstringthe placeholder start keyword
phEndstringthe placeholder end keyword

Example

// returns 123456
pav.discardPlaceholders('1234${placeholder}56','${','}')

extractPlaceholders(template, phStart, phEnd) ⇒ array

Extract the placeholders of a string template.

Kind: global function
Returns: array - all the placeholders

ParamTypeDescription
templatestringlist of strings or objects
phStartstringthe placeholder start keyword
phEndstringthe placeholder end keyword

Example

// returns placeholder
pav.extractPlaceholders('1234${placeholder}56','${','}')

getArrInArr(arrIdx, listOfList) ⇒ array

Gets an array using an array of indexes

Kind: global function
Returns: array - the selected list

ParamTypeDescription
arrIdxarrayan array of indices
listOfListarraya list of list

Example

// returns ['b', '1']
pav.getArrInArr([1, 1], [['a','b'], [1, 2]])

decArrayIndex(arrIdx, maxIdx) ⇒ array

Decrements an array of indexes from left to right. For right to left, uses .reverse(). This is useful for iterating over indices.

Kind: global function
Returns: array - the selected list

ParamTypeDescription
arrIdxarrayan array of indices
maxIdxarrayan array of the maximum indices

Example

// returns [1, 2, 4]
pav.decArrayIndex([2, 2, 4], [3, 2, 4])

combineListOfList(listOfList) ⇒ array

Combine a list of list in a similar as imbricated for loops

Kind: global function
Returns: array - All the possible combinations in reverse order

ParamTypeDescription
listOfListarraya list of list

Example

// returns [['b', 2], ['a', 2], ['b', 1], ['a', 1]]
pav.combineListOfList([['a','b'], [1, 2]])

highestRankedCombination(listCombination, rankFn, filterFn) ⇒ array

Finds the combination with the highest rank

Kind: global function
Returns: array - The highest ranked combination

ParamTypeDescription
listCombinationarraya list of list
rankFnfunctiona function which returns the rank. Default by size
filterFnfunctiona function which filter only the suitable combination.

Example

// returns ['bb', 22]
pav.highestRankedCombination([['bb', 2], ['a', 22], ['b', 1], ['a', 1]])

coalesce(fns, value) ⇒ array

Run functions sequentially until one succeeds or return null.

Kind: global function
Returns: array - The result of applying the passed function

ParamTypeDescription
fnsarraya list of functions
valueobjecta value to be passed to each function

Example

// returns the result of f1('value') otherwise the value of f2('value')
pav.coalesce([f1, f2], 'value')

extractValuesFromPaths(props, data) ⇒ object

Uses json paths mapping to transform an object

Kind: global function
Returns: object - An object with the extracted data

ParamTypeDescription
propsobjectdescribe each property with a list of paths. Optionally, the first element can be a transformer function.
dataobjectthe data to extract the values from

Example

// returns { a: ['3', '4'], b: '13' }
const x13 = value => value*13;
const data = {q: '1', p: {a: '3', b: '4'}}
pav.extractValuesFromPaths({ a: ['p.a', 'p.b'], b: [x13, 'q'] }, data)

voidTemplate(placeholders4clean, template) ⇒ object

Creates a template without specific placeholders

Kind: global function
Returns: object - a template without these placeholders

ParamTypeDescription
placeholders4cleanarraya list of placeholders'start and end.
templatestringthe template

Example

// returns AABB
pav.voidTemplate([['<a>{{', '}}</a>']], 'AA<a>{{jdsljals}}</a>BB')

getTemplateParams(propsData, placeholders, selected) ⇒ object

Build template parameters based on given placeholders

Kind: global function
Returns: object - the merging of relevant parameters with default ones.

ParamTypeDescription
propsDataobjectdefault parameters for the template
placeholdersarraya list of placeholder names
selectedarraythe values associated with placeholder

Example

// return { a: 'X', b: 'C', c: 'D', d: 'Y' }
pav.getTemplateParams({ a: ['A', 'B'], b: ['C'], c: [null, 'D', 'E'],
d: 'G' }, ['a', 'd'], ['X', 'Y'])

renderFittest(conf, data, selector) ⇒ string

Render text based on a template with selection of the most suited (fit) parameters.

Kind: global function
Returns: string - the rendered template

ParamTypeDescription
confobjectconfiguration of the renderer
dataobjectthe live data
selectorfunctiona function which return the best selection of parameters

Example

// return <a>k1v</a> to <b>K3V</b> to <c>k4v</c>
  const tUpper = value => value.toUpperCase();
  const conf = {
   templates: ['<a>{{a}}</a> to <b>{{b}}</b> to <c>{{c}}</c>',
     '<b>{{a}}</b> to <c>{{b}}</c>'],
   props: { a: ['k1', 'k2'], b: [tUpper, 'k3'], c: ['k4', 'k5', 'k1'] },
   placeholders: {
     clean: [['<a>{{', '}}</a>']],
     extract: [['<b>{{', '}}</b>'], ['<c>{{', '}}</c>']],
   },
 };

 const data = {
   k1: 'k1v',
   k2: 'k2v',
   k3: 'k3v',
   k4: 'k4v',
 };
pav.renderFitest(conf, data, selector)

renderLongest(conf, data, max) ⇒ string

Render text based on a template with selection of the longest parameters.

Kind: global function
Returns: string - the rendered template

ParamTypeDescription
confobjectconfiguration of the renderer
dataobjectthe live data
maxintegermaximum length of the generated string

Example

const tUpper = value => value.toUpperCase();
 const conf = {
   templates: ['<a>{{a}}</a> to <b>{{b}}</b> to <c>{{c}}</c>',
     '<c>{{c}}</c>'],
   props: { a: ['k1', 'k2'], b: [tUpper, 'k3'], c: ['k4', 'k5', 'k6'] },
   placeholders: {
     clean: [['<a>{{', '}}</a>']],
     extract: [['<b>{{', '}}</b>'], ['<c>{{', '}}</c>']],
   },
 };

 const data = {
   k1: 'k1v1',
   k2: 'k1v11',
   k3: 'k3v',
   k4: 'k4v',
   k6: 'k4v66',
 };
 pav.renderLongest(conf, data)
 // return <a>k1v1</a> to <b>K3V</b> to <c>k4v66</c>
 pav.renderLongest(conf, data, 10)
 // return <c>k4v</c>

License

MIT © olih