pick-alternate-value
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-valueUsage
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 
- pickShortestSize(list, defaultValue, min) ⇒ object
- Pick the first string (or object) with the shortest size 
- sumSize(list) ⇒ integer
- Adds the size of all the items in the list 
- minSize(list) ⇒ integer
- Finds the minimum size of all the items in the list 
- maxSize(list) ⇒ integer
- Finds the maximum size of all the items in the list 
- hasNoNull(list) ⇒ integer
- Returns true if the list does not contain any null 
- discardPlaceholders(template, phStart, phEnd) ⇒ string
- Discard the placeholders of a string template. Useful to check the minimum length of a template. 
- extractPlaceholders(template, phStart, phEnd) ⇒ array
- Extract the placeholders of a string template. 
- getArrInArr(arrIdx, listOfList) ⇒ array
- Gets an array using an array of indexes 
- 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. 
- combineListOfList(listOfList) ⇒ array
- Combine a list of list in a similar as imbricated for loops 
- highestRankedCombination(listCombination, rankFn, filterFn) ⇒ array
- Finds the combination with the highest rank 
- coalesce(fns, value) ⇒ array
- Run functions sequentially until one succeeds or return null. 
- extractValuesFromPaths(props, data) ⇒ object
- Uses json paths mapping to transform an object 
- voidTemplate(placeholders4clean, template) ⇒ object
- Creates a template without specific placeholders 
- getTemplateParams(propsData, placeholders, selected) ⇒ object
- Build template parameters based on given placeholders 
- renderFittest(conf, data, selector) ⇒ string
- Render text based on a template with selection of the most suited (fit) parameters. 
- renderLongest(conf, data, max) ⇒ string
- Render text based on a template with selection of the longest parameters. 
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  
| Param | Type | Description | 
|---|---|---|
| list | array | list of strings or objects | 
| defaultValue | object | the object/string to return if null | 
| max | integer | the 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  
| Param | Type | Description | 
|---|---|---|
| list | array | list of strings or objects | 
| defaultValue | object | the object/string to return if null | 
| min | integer | the 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  
| Param | Type | Description | 
|---|---|---|
| list | array | list 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  
| Param | Type | Description | 
|---|---|---|
| list | array | list 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  
| Param | Type | Description | 
|---|---|---|
| list | array | list 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  
| Param | Type | Description | 
|---|---|---|
| list | array | list 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  
| Param | Type | Description | 
|---|---|---|
| template | string | list of strings or objects | 
| phStart | string | the placeholder start keyword | 
| phEnd | string | the 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  
| Param | Type | Description | 
|---|---|---|
| template | string | list of strings or objects | 
| phStart | string | the placeholder start keyword | 
| phEnd | string | the 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  
| Param | Type | Description | 
|---|---|---|
| arrIdx | array | an array of indices | 
| listOfList | array | a 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  
| Param | Type | Description | 
|---|---|---|
| arrIdx | array | an array of indices | 
| maxIdx | array | an 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  
| Param | Type | Description | 
|---|---|---|
| listOfList | array | a 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  
| Param | Type | Description | 
|---|---|---|
| listCombination | array | a list of list | 
| rankFn | function | a function which returns the rank. Default by size | 
| filterFn | function | a 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  
| Param | Type | Description | 
|---|---|---|
| fns | array | a list of functions | 
| value | object | a 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  
| Param | Type | Description | 
|---|---|---|
| props | object | describe each property with a list of paths. Optionally, the first element can be a transformer function. | 
| data | object | the 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  
| Param | Type | Description | 
|---|---|---|
| placeholders4clean | array | a list of placeholders'start and end. | 
| template | string | the 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.  
| Param | Type | Description | 
|---|---|---|
| propsData | object | default parameters for the template | 
| placeholders | array | a list of placeholder names | 
| selected | array | the 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  
| Param | Type | Description | 
|---|---|---|
| conf | object | configuration of the renderer | 
| data | object | the live data | 
| selector | function | a 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  
| Param | Type | Description | 
|---|---|---|
| conf | object | configuration of the renderer | 
| data | object | the live data | 
| max | integer | maximum 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
 flarebyte
flarebyte