Skip to content

jheagle/si-funciona

Repository files navigation

Funciones de utilidad para uso general. [General usage utility functions.]

Modules

arrayHelpers

Some simple utility functions for generating arrays or performing work on arrays.

objectDescriptors

Create a format to standardize every object into a specific template.

functionHelpers

Manage how functions are called with these utilities.

numberHelpers

Some number comparators and random number generators.

objectHelpers

Simplify working with object by providing array-like parsing. Also, provides cloning and merging along with accessors that always have a return value for optimal nesting.

stringHelpers

Manage how strings are manipulated with these utilities.

siFunciona

All the siFunciona system functions for stringing together functions and simplifying logic.

arrayHelpers

Some simple utility functions for generating arrays or performing work on arrays.

Version: 1.0.0
Author: Joshua Heagle [email protected]

arrayHelpers.BasicQueue

Class BasicQueue is a functional example of a queue to be used with queueManager.

Kind: static class of arrayHelpers

basicQueue.dequeue() ⇒ queuedItem | *

Remove and return the next item in the queue

Kind: instance method of BasicQueue

basicQueue.empty() ⇒ boolean

Check if the queue is empty

Kind: instance method of BasicQueue

basicQueue.enqueue(data) ⇒ BasicQueue

Add an item to the end of the queue

Kind: instance method of BasicQueue

Param Type
data queuedItem | *

basicQueue.peek() ⇒ queuedItem | *

Retrieve the next item from the queue

Kind: instance method of BasicQueue

basicQueue.size() ⇒ number

Get the quantity of items in the queue

Kind: instance method of BasicQueue

arrayHelpers.addUniqueToArray(item, array) ⇒ Array

Having an array and a potential new array element, check if the element is in the array, if not append to array.

Kind: static method of arrayHelpers

Param Type Description
item * An potential array element, possibly a DomItem
array Array An array where an element may be appended.

arrayHelpers.buildArray(item, length) ⇒ Array.<*>

Leverage buildArrayBase to generate an array filled with a copy of the provided item. The length defines how long the array should be.

Kind: static method of arrayHelpers

Param Type Description
item * The item to be used for each array element
length number The desired length of the array

arrayHelpers.buildArrayOfReferences(item, length) ⇒ Array.<*>

Leverage buildArrayBase to generate an array filled with references to the provided item. The length defines how long the array should be.

Kind: static method of arrayHelpers

Param Type Description
item * The item to be used for each array element
length number The desired length of the array

arrayHelpers.compareArrays(...arrays) ⇒ Array.<module:arrayHelpers~compareArrayResult>

Compare two Arrays and return the Object where the value for each property is as follows: -1 to indicate val1 is less than val2 0 to indicate both values are the equal 1 to indicate val1 is greater than val2 The returned Object uses the element values as the property names This functions works by first creating a concatenated array of all unique values. Then for each unique values, convert to a string and use it as a new property name. Array filter each array checking if it has the unique value. Use the lengths of these filtered arrays to compare. So if the first array has the value and the second one doesn't the first length will be one or more and the second will be zero, if the both have the value then both will be one or more.

Kind: static method of arrayHelpers

Param Type Description
...arrays Array The arrays to compare

Example

// example of input and resulting output
compareArrays(
  ['match1', 'firstMismatch1', 'match2', 'firstMismatch2', 'badMatch1'],
  ['match1', 'match2', 'secondMismatch1', 'badMatch1', 'badMatch1']
)
// unique array
['match1', 'firstMismatch1', 'match2', 'firstMismatch2', 'badMatch1', 'secondMismatch1']
// result object
[
  {
    value: 'match1',
    keys: [[0], [0]],
    result: [0, 0]
  },
  {
    value: 'firstMismatch1',
    keys: [[1], []],
    result: [1, -1]
  },
  {
    value: 'match2',
    keys: [[2], [1]],
    result: [0, 0]
  },
  {
    value: 'firstMismatch2',
    keys: [[3], []],
    result: [1, -1]
  },
  {
    value: 'badMatch1',
    keys: [[4], [3, 4]],
    result: [0, 0]
  },
  {
    value: 'secondMismatch1',
    keys: [[], [2]],
    result: [-1, 1]
  }
]

arrayHelpers.mergeArrays(...arrays) ⇒ Array

Take multiple arrays and then filter all these into one unique array.

Kind: static method of arrayHelpers

Param Type Description
...arrays Array Provide multiple arrays to create one unique array

arrayHelpers.uniqueArray(array) ⇒ Array

Remove duplicate values from an array. uniqueArray

Kind: static method of arrayHelpers

Param Type Description
array Array The array to make unique

objectDescriptors

Create a format to standardize every object into a specific template.

Version: 1.0.0
Author: Joshua Heagle [email protected]

objectDescriptors.descriptorSample : module:objectDescriptors~descriptor

Kind: static constant of objectDescriptors

objectDescriptors.descriptorDetailSample : module:objectDescriptors~descriptorDetail

Kind: static constant of objectDescriptors

objectDescriptors.descriptorMapSample : module:objectDescriptors~descriptorMap

Kind: static constant of objectDescriptors

objectDescriptors.mappedDescriptorMap : module:objectDescriptors~descriptorMap

Kind: static constant of objectDescriptors

objectDescriptors.assignDescriptor(originalMap, ...descriptors) ⇒ module:objectDescriptors~descriptor

Apply one or more descriptors to an existing descriptor so that they represent a merged version of the descriptors.

Kind: static method of objectDescriptors

Param Type
originalMap module:objectDescriptors~descriptor
...descriptors module:objectDescriptors~descriptor

objectDescriptors.assignDescriptorDetail(originalDetail, ...details) ⇒ module:objectDescriptors~descriptorDetail

Assign properties from other details onto an existing detail.

Kind: static method of objectDescriptors

Param Type
originalDetail module:objectDescriptors~descriptorDetail
...details module:objectDescriptors~descriptorDetail

objectDescriptors.checkClearValues(descriptor, [keepValues]) ⇒ module:objectDescriptors~descriptor

Check if we should clear the values on this descriptor

Kind: static method of objectDescriptors

Param Type Default
descriptor module:objectDescriptors~descriptor
[keepValues] boolean false

objectDescriptors.checkDescriptorComplete(descriptor) ⇒ module:objectDescriptors~descriptor

Check if the descriptors references have all been built and set complete to true if they have.

Kind: static method of objectDescriptors

Param Type
descriptor module:objectDescriptors~descriptor

objectDescriptors.cloneDescriptor(originalMap) ⇒ module:objectDescriptors~descriptor

Make a copy of an object descriptor so that the original will not be mutated.

Kind: static method of objectDescriptors

Param Type
originalMap module:objectDescriptors~descriptor

objectDescriptors.cloneDescriptorDetail(originalDetail) ⇒ module:objectDescriptors~descriptorDetail

Get a new copy of an existing Descriptor Detail

Kind: static method of objectDescriptors

Param Type
originalDetail module:objectDescriptors~descriptorDetail

objectDescriptors.compareDescriptor(descriptor1, descriptor2) ⇒ boolean

Check if two descriptors are the same or similar in that they have similar keys and the associated types are the same.

Kind: static method of objectDescriptors

Param Type
descriptor1 module:objectDescriptors~descriptor
descriptor2 module:objectDescriptors~descriptor

objectDescriptors.describeObject(object) ⇒ module:objectDescriptors~descriptor

Trace an object and return the descriptor which defines the object's structure and attributes.

Kind: static method of objectDescriptors

Param Type
object Object

objectDescriptors.describeObjectDetail(value, [key], [index]) ⇒ module:objectDescriptors~descriptorDetail

Trace an object's attribute and provide details about it.

Kind: static method of objectDescriptors

Param Type Default
value *
[key] string | number 0
[index] number 0

objectDescriptors.describeObjectMap(object, [options]) ⇒ module:objectDescriptors~descriptorMap

Trace out the entire object including nested objects.

Kind: static method of objectDescriptors

Param Type Default
object Object | Array
[options] Object {}
[options.mapLimit] number 1000000000
[options.depthLimit] number -1
[options.keepValues] boolean false

objectDescriptors.nextReference(descriptor, currentReference) ⇒ number | undefined

Find the index of the next module:objectDescriptors.descriptorDetail to build a resource for.

Kind: static method of objectDescriptors

Param Type
descriptor module:objectDescriptors~descriptor
currentReference number

objectDescriptors.sameDescriptor(descriptor1, descriptor2) ⇒ boolean

Check if the two descriptors are the same.

Kind: static method of objectDescriptors

Param Type
descriptor1 module:objectDescriptors~descriptor
descriptor2 module:objectDescriptors~descriptor

functionHelpers

Manage how functions are called with these utilities.

Version: 1.0.0
Author: Joshua Heagle [email protected]

functionHelpers.callWithParams(fn, params, [minimum]) ⇒ *

Given a function, call with the correct number of parameters from an array of possible parameters.

Kind: static method of functionHelpers

Param Type Default Description
fn function The function to be called
params Array Array of possible function parameters
[minimum] number 2 Minimum number of parameters to use in the function

functionHelpers.curry(fn) ⇒ function | *

Return a curried version of the passed function. The returned function expects the same number of arguments minus the ones provided. fn is the name of the function being curried.

Kind: static method of functionHelpers

Param Type Description
fn function Receives a function to be curried

functionHelpers.delay(time) ⇒ module:functionHelpers~delayHandler

Provide a timeout which returns a promise.

Kind: static method of functionHelpers

Param Type Description
time number Delay in milliseconds

functionHelpers.makeBasicQueue(initialQueue) ⇒ IsQueue

Create an instance of a basic queue.

Kind: static method of functionHelpers

Param Type
initialQueue Array

functionHelpers.onBodyLoad(callback, [reset]) ⇒ Array.<function()>

Prepare functions to be called once the body is available.

Kind: static method of functionHelpers

Param Type Default
callback function
[reset] boolean false

functionHelpers.pipe(...fns) ⇒ *

Take one or more function with a single parameter and return value. Pass a parameter and the value will be transformed by each function then returned.

Kind: static method of functionHelpers

Param Type Description
...fns function Takes a series of functions having the same parameter

functionHelpers.preloadParams(fn, params, [unassignedParam]) ⇒ module:functionHelpers~callWithMissing

Provide an array of parameters to be used with a function, allow the function to be called later with the missing parameter.

Kind: static method of functionHelpers

Param Type Default Description
fn function The function to be called
params Array The parameters to preload
[unassignedParam] number 0 Position of missing parameter (zero indexed)

functionHelpers.queueManager([queue]) ⇒ module:functionHelpers~queueManagerHandle

Manage functions to run sequentially.

Kind: static method of functionHelpers

Param Type Default Description
[queue] IsQueue [] The iterable that can be used to store queued functions

queueManager~makeQueuedRunnable(resolve, reject, fn, ...args) ⇒ queuedRunnable

Convert a function to a queueable object.

Kind: inner method of queueManager

Param Type
resolve Promise.resolve
reject Promise.reject
fn function
...args *

queueManager~postRun(result) ⇒ *

After an item is run, THEN run this function to reset isRunning

Kind: inner method of queueManager

Param Type
result *

queueManager~runNextItem() ⇒ IteratorYieldResult | null

When ready, runs the next queued runnable generator.

Kind: inner method of queueManager

queueManager~pushAnother(fn, ...args) ⇒

Add a function into the queue to be run when ready.

Kind: inner method of queueManager
Returns: Promise

Param Type Description
fn function The function to run when ready
...args * Optional arguments to apply when the function is ready to be run

functionHelpers.queueTimeout([queueManagerHandle]) ⇒ module:functionHelpers~queueTimeoutHandle

Manage functions to run sequentially with delays.

Kind: static method of functionHelpers

Param Type Default
[queueManagerHandle] module:functionHelpers~queueManagerHandle

functionHelpers.relevancyFilter(map, [options]) ⇒ relevanceMap

Remove elements out of relevance range and update the max relevance.

Kind: static method of functionHelpers

Param Type Default
map relevanceMap
[options] Object {}
[options.mapLimit] int 1000
[options.relevancyRange] int 100

functionHelpers.trace(label, useClone) ⇒ function

Output the value with label to the console and return the value to not interrupt the code.

Kind: static method of functionHelpers

Param Type Description
label string Pass an identifying label of the value being output.
useClone Determines if the logged data should be a clone of the original to preserve state.

numberHelpers

Some number comparators and random number generators.

Version: 1.0.0
Author: Joshua Heagle [email protected]

numberHelpers.absoluteMax(num1, num2) ⇒ number

Helper for returning the absolute max value

Kind: static method of numberHelpers

Param Type Description
num1 number A number to compare
num2 number Another number to be compared against

numberHelpers.absoluteMin(num1, num2) ⇒ number

Helper for returning the absolute min value

Kind: static method of numberHelpers

Param Type Description
num1 number A number to compare
num2 number Another number to be compared against

numberHelpers.compare(val1, val2) ⇒ number

Compare two numbers and return: -1 to indicate val1 is less than val2 0 to indicate both values are the equal 1 to indicate val1 is greater than val2

Kind: static method of numberHelpers

Param Type Description
val1 number The first number to compare
val2 number The second number to compare

numberHelpers.randomInteger(range, [offset], [interval]) ⇒ number

Create a single random integer within provide range. And with optional offset, The distance between the result numbers can be adjusted with interval.

Kind: static method of numberHelpers

Param Type Default Description
range number Choose the breadth of the random number (0-100 would be 100 for range)
[offset] number 0 Choose the starting number (1-10 would be 1 for offset, 9 for range)
[interval] number 1 Choose the distance between numbers (5, 10, 15 would be 5 for interval, 1 for offset, 2 for range)

numberHelpers.randomNumber(range, [offset], [interval]) ⇒ number

Create a single random number within provided range. And with optional offset, The distance between the result numbers can be adjusted with interval.

Kind: static method of numberHelpers

Param Type Default Description
range number Choose the breadth of the random number (0-100 would be 100 for range)
[offset] number 0 Choose the starting number (1-10 would be 1 for offset, 9 for range)
[interval] number 1 Choose the distance between numbers (~5, ~10, ~15 would be 5 for interval, 1 for offset, 2 for range)

objectHelpers

Simplify working with object by providing array-like parsing. Also, provides cloning and merging along with accessors that always have a return value for optimal nesting.

Version: 1.0.0
Author: Joshua Heagle [email protected]

objectHelpers.mergeObjects ⇒ *

Uses mergeObjectsBase deep merge objects and arrays, merge by value.

Kind: static constant of objectHelpers
See: module:objectHelpers~mergeObjectsCallback

Param Type Description
...objects Object Provide a list of objects which will be merged starting from the end up into the first

objectHelpers.mergeObjectsMutable ⇒ *

Uses mergeObjectsBase deep merge objects and arrays, merge by reference.

Kind: static constant of objectHelpers
See: module:objectHelpers~mergeObjectsCallback

Param Type Description
...objects Object Provide a list of objects which will be merged starting from the end up into the first

objectHelpers.cloneObject(object, [options]) ⇒ Object

Clone objects for manipulation without data corruption, returns a copy of the provided object. NOTE: Use the mapLimit and relevancyRange to resolve "too much recursion" when the object is large and is known to have circular references. A high mapLimit may lead to heavy memory usage and slow performance.

Kind: static method of objectHelpers

Param Type Default Description
object Object The original object that is being cloned
[options] Object {}
[options.mapLimit] number 100 Size of temporary reference array used in memory before assessing relevancy.
[options.depthLimit] number -1 Control how many nested levels deep will be used, -1 = no limit, >-1 = nth level limited.
[options.relevancyRange] number 1000 Total reference map length subtract this range, any relevancy less than that amount at time of evaluation will be removed.

objectHelpers.dotGet(arrayObject, dotNotation, [defaultValue]) ⇒ *

Get a nested property value from an object.

Kind: static method of objectHelpers
Returns: * - The value of the property

Param Type Default Description
arrayObject Object The array or object to get the property from
dotNotation string The path to the property
[defaultValue] string | null null The default value to return if the property is not found

objectHelpers.dotNotate(arrayObject, [retainObjects]) ⇒ DotNotatedObject

Convert an array or object to a single dimensional associative array with dot notation.

Kind: static method of objectHelpers
Returns: DotNotatedObject - The dot-notated object

Param Type Default Description
arrayObject Object The array or object to dot-notate
[retainObjects] Array.<DotNotationString> [] An array of keys to retain as objects

objectHelpers.dotSet(arrayObject, dotNotation, value) ⇒ Object

Set a nested property value an object.

Kind: static method of objectHelpers
Returns: Object - The modified object

Param Type Description
arrayObject Object The array or object to set the property on
dotNotation string The path for the property
value * The default value to return if the property is not found

objectHelpers.dotUnset(arrayObject, dotNotation) ⇒ Object

Unset a nested property value an object.

Kind: static method of objectHelpers
Returns: Object - The modified object

Param Type Description
arrayObject Object The array or object to set the property on
dotNotation string The path for the property

objectHelpers.emptyObject(item) ⇒ boolean

Helper function for testing if the item is an Object or Array that does not have any properties

Kind: static method of objectHelpers

Param Type Description
item Object | Array Object or Array to test

objectHelpers.filterObject(obj, fn, [thisArg]) ⇒ Object | Array

This function is intended to replicate behaviour of the Array.filter() function but for Objects. If an array is passed in instead then it will perform standard filter(). It is recommended to always use the standard filter() function when it is known that the object is actually an array.

Kind: static method of objectHelpers

Param Type Description
obj Object | Array The Object (or Array) to be filtered
fn module:objectHelpers~filterCallback | function The function to be processed for each filtered property
[thisArg] Object | Array Optional. Value to use as this when executing callback.

objectHelpers.isCloneable(value) ⇒ boolean

Determine if the value is a reference instance

Kind: static method of objectHelpers

Param Type
value Array | Object | *

objectHelpers.isInstanceObject(object) ⇒ boolean

Check if the current object has inherited properties.

Kind: static method of objectHelpers

Param Type
object Object | Array

objectHelpers.isObject(object) ⇒ boolean

Check if the provided thing is an object / array.

Kind: static method of objectHelpers

Param Type
object *

objectHelpers.mapObject(obj, fn, [thisArg]) ⇒ Object | Array

This function is intended to replicate behaviour of the Array.map() function but for Objects. If an array is passed in instead then it will perform standard map(). It is recommended to always use the standard map() function when it is known that the object is actually an array.

Kind: static method of objectHelpers

Param Type Description
obj Object | Array The Object (or Array) to be mapped
fn module:objectHelpers~mapCallback | function The function to be processed for each mapped property
[thisArg] Object | Array Optional. Value to use as this when executing callback.

objectHelpers.mergeObjectsBase([options]) ⇒ module:objectHelpers~mergeObjectsCallback | mergeObjectsCallback

Perform a deep merge of objects. This will return a function that will combine all objects and sub-objects. Objects having the same attributes will overwrite from last object to first. NOTE: Use the mapLimit and relevancyRange to resolve "too much recursion" when the object is large and is known to have circular references. A high mapLimit may lead to heavy memory usage and slow performance.

Kind: static method of objectHelpers

Param Type Default Description
[options] Object {}
[options.mapLimit] number 100 Size of temporary reference array used in memory before assessing relevancy.
[options.depthLimit] number -1 Control how many nested levels deep will be used, -1 = no limit, >-1 = nth level limited.
[options.relevancyRange] number 1000 Total reference map length subtract this range, any relevancy less than that amount at time of evaluation will be removed.
[options.map] Iterable | array [] A predetermined list of references gathered (to be passed to itself during recursion).
[options.useClone] boolean false

objectHelpers.objectKeys(object, [includeInherited]) ⇒ Array.<(string|number)>

Get an array of keys from any object or array. Will return empty array when invalid or there are no keys. Optional flag will include the inherited keys from prototype chain when set.

Kind: static method of objectHelpers

Param Type Default
object Object | Array
[includeInherited] boolean false

objectHelpers.objectValues(object, [includeInherited]) ⇒ Array

Get an array of values from any object or array. Will return empty array when invalid or there are no values. Optional flag will include the inherited values from prototype chain when set.

Kind: static method of objectHelpers

Param Type Default
object Object | Array
[includeInherited] boolean false

objectHelpers.reduceObject(obj, fn, [initialValue]) ⇒ *

This function is intended to replicate behaviour of the Array.reduce() function but for Objects. If an array is passed in instead then it will perform standard reduce(). It is recommended to always use the standard reduce() function when it is known that the object is actually an array.

Kind: static method of objectHelpers

Param Type Description
obj Object | Array The Object (or Array) to be filtered
fn module:objectHelpers~reduceCallback | function | reduceCallback The function to be processed for each filtered property
[initialValue] Object | Array Optional. Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used. Calling reduce on an empty array without an initial value is an error.

objectHelpers.setAndReturnValue(item, key, value) ⇒ *

Set a value on an item, then return the value

Kind: static method of objectHelpers

Param Type Description
item Object | Array An object or array to be updated
key string | number The key on the item which will have its value set
value * Any value to be applied to the key

objectHelpers.setValue(key, value, item) ⇒ Object | Array

Set a value on an item, then return the item. NOTE: Argument order designed for usage with pipe

Kind: static method of objectHelpers

Param Type Description
key string | number The key on the item which will have its value set
value * Any value to be applied to the key
item Object | Array An object or array to be updated

objectHelpers~handleRetainObjects([retainObjects]) ⇒ function

Convert an array of keys into a regex, return a function to test if incoming keys match.

Kind: inner method of objectHelpers
Returns: function - The dot-notated array

Param Type Default Description
[retainObjects] Array.<DotNotationString> [] An array of keys to retain as objects

objectHelpers~performDotNotate(arrayObject, didRetain, [prepend], [results]) ⇒ DotNotatedObject

The underlying logic function for converting arrays to dot-notation.

Kind: inner method of objectHelpers
Returns: DotNotatedObject - The dot-notated object

Param Type Default Description
arrayObject Object The array or object to dot-notate
didRetain function The test function to see if a key should be retained
[prepend] DotNotationString '' The path for the property being processed
[results] DotNotatedObject {} The final array to return

stringHelpers

Manage how strings are manipulated with these utilities.

Version: 1.0.0
Author: Joshua Heagle [email protected]

stringHelpers.camelCase(str) ⇒ string

Given a string in kebab-case, snake_case or 'Sentence case', convert to camelCase.

Kind: static method of stringHelpers

Param Type
str string

stringHelpers.kabobCase(str) ⇒ string

Given a string in snake_case, camelCase or 'Sentence case', convert to kabob-case.

Kind: static method of stringHelpers

Param Type
str string

stringHelpers.makeFilepath(root, [append]) ⇒ string

Format the given path so that it does not have trailing slashes and also correctly appends a path.

Kind: static method of stringHelpers

Param Type Default
root string
[append] string "''"

stringHelpers.makeRelativePath(fromFile, toFile) ⇒ string

Compare two file paths and simplify them to a relative path.

Kind: static method of stringHelpers

Param Type
fromFile string
toFile string

stringHelpers.regexEscape(str) ⇒ string

Take a string and escape the regex characters.

Kind: static method of stringHelpers

Param Type
str string

stringHelpers.snakeCase(str) ⇒ string

Given a string in kebab-case, camelCase or 'Sentence case', convert to snake_case.

Kind: static method of stringHelpers

Param Type
str string

stringHelpers.strAfter(str, search) ⇒ string

Retrieve the string part after the search match.

Kind: static method of stringHelpers

Param Type
str string
search string

stringHelpers.strAfterLast(str, search) ⇒ string

Retrieve the string part after the last search match.

Kind: static method of stringHelpers

Param Type
str string
search string

stringHelpers.strBefore(str, search) ⇒ string

Retrieve the string part before the search match.

Kind: static method of stringHelpers

Param Type
str string
search string

stringHelpers.strBeforeLast(str, search) ⇒ string

Retrieve the string part after the last search match.

Kind: static method of stringHelpers

Param Type
str string
search string

stringHelpers.titleCase(str) ⇒ string

Given a string in kebab-case, snake_case, camelCase or 'Sentence case', convert to 'Title Case'.

Kind: static method of stringHelpers

Param Type
str string

stringHelpers.ucFirst(str) ⇒ string

Given a string, make the first character uppercase and the rest lowercase.

Kind: static method of stringHelpers

Param Type
str string

stringHelpers.words(str) ⇒ array

Split a string into sets of numbers or letters.

Kind: static method of stringHelpers

Param Type
str string

siFunciona

All the siFunciona system functions for stringing together functions and simplifying logic.

Version: 1.0.0
Author: Joshua Heagle [email protected]