G4 functions module
Module of custom functions
Table of Contents
Prerequisites
Installation
Package installation
> npm install @intelisis/g4-functions --save
Browser include
Usage
Initialize
To initialize a new navigation region:
// JS Exampleconst g4 = g4
# Coffee Example g4 = g4
String funcions
upperCase(stringToUppercase)
stringToUppercase
Type: String
Changes the case of the whole string.
result g4upperCase'intelisis'# returns: INTELISIS
left(string, numberCharacters)
string
Type: String
numberCharacters
Type: Number
- characters defaults to: 1
Returns the first n characters of String
result g4left'Automoriz'3# returns: Aut result g4left'F1238432'# returns: F
right(string, numberCharacters)
string
Type: String
numberCharacters
Type: Number
- characters defaults to: 1
Returns the last n characters of String
result g4right'Intelisis'3# returns: sis result g4right'Logitech'# returns: h
contains(needle, haystack)
needle
Type: String
, Object
or Array
haystack
Type: String
Returns true if the haystack contains the needle otherwise return false
# search string inside an array needle = 'Intelisis'haystack = 'foo''Intelisis''bar'result g4containshaystackneedle# returns: true
# search string inside array object's values needle = 'Intelisis'haystack = name: 'Intelisis is cool' name: 'Apple is cooler'result g4containshaystackneedle# returns: true
# can't find a string inside an array needle = 'G4'haystack = 'foo''Intelisis''bar'result g4containshaystackneedle# returns: false
Number functions
sum(tagetToSum)
tagetToSum
Type: Array
or Number
Sum the values of a matrix or a list with values. In case of a (multi dimensional) array or matrix, the sum of all elements will be calculated.
- [[2, 5], [4, 3], [1, 7]] returns:
22
result g4sum1572# returns: 15 result g4sum2143# returns: 10
round(NumberToRound, [decimals])
NumberToRound
Type: Number
decimals
Type: Number
- decimals defaults to: 2
Rounds a number up and down, optionally accepts the number of decimals by which to round.
result g4round5.5234# returns: 5.52 result g4round5.52343# returns: 5.523
floor(NumberToFloor)
NumberToFloor
Type: Number
Floors a Number to the ground
result g4floor123.45# returns: 123 result g4floor-5.1# returns -6
getNumericPercentage(diversityString)
diversityString
Type: String
Returns the number for percentage given a diversity of strings:
- 'exento' returns:
0
- 16% returns:
0.016
- 9% returns:
0.009
- 0% returns:
0
Iva = 'exento'result g4getNumericPercentageIVA# returns: 0 Iva = '16%'result g4getNumericPercentageIVA# returns: 0.016
Data-objects
copyToColumn(sectionField, String)
sectionField
Type: String
String
Type: String
diversityString
Type: String
Copies Section.Field to all the rows of an ArraySection.Field
g4copyToColumnDatosGeneralesFolioDetalle'Reference'# Copied the DatosGenerales.Folio -field's value # to every Reference field in the Detalle Array.
findRow(List, Row)
List
Type: Array
Row
Type: String
Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.
list = name: 'Intelisis' path : './about.html' result g4findRowlist'Intelsis'# returns: { name: 'Intelisis' }
getColumn(List, columnToFind)
List
Type: Array
columnToFind
Type: String
Extracting a list of property values.
list = name: 'Moe' name: 'Intelsis' result g4getColumnlist'name'# returns: ['Moe' 'Intelisis']
map(ArrayToApply, functionToApply)
ArrayToApply
Type: String
functionToApply
Type: function
Produces a new array of values by mapping each value in list through a transformation function (iteratee).
list = 149result g4maplist return num * 3# returns: [3, 12, 27]
reject(List, functionToApply)
columnToFind
Type: String
Returns the values in list without the items that meet the condition.
list = 123456result g4rejectlist return num % 2 == 0# returns: [1, 3, 5]
filter(List, functionToApply)
List
Type: Array
functionToApply
Type: function
Returns the values in list with the items that meet the condition.
list = 123456result g4filterlist return num % 2 == 0# returns: [2,4,6]
find(List, functionToApply)
List
Type: Array
functionToApply
Type: function
Returns the first value found to comply with the condition, or undefined if no found value to comply with the condition. The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.
list = 123456result g4findlist return num % 2 == 0# returns 2
reduce(List, functionToApply)
List
Type: Array
functionToApply
Type: function
Boils down a list of values into a single value. Memo is the initial state of the reduction, and each successive step of it should be returned by iteratee.
list = 123result g4reducelist memo + num# returns: 6
deep(List, keys)
List
Type: Array
keys
Type: String
Returns the final value of the indicated keys.
obj = a: b: c: 'Intelisis' result g4deepobj'a.b.c'# returns: 'Intelisis'
documents().updateSection(JSON) (process only)
JSON
Type: JSON
Updates one or more columns in an array type section, with an equal number of values.
This function is asynchronous, so it needs to be called with the exec/into functions.
options = step: 'general.Task 2' section : 'section1' columns : 'input2' values:'valor raul' exec g4documentsupdateSection optionsinto errresultreturn callback errresult# returns: 1 or Error
catalogs().publishAndAffect(JSON) (process only)
JSON
Type: Object
Publish a new catalog item, both in NoSQL and in Kernel
This function is asynchronous, so it needs to be called with the exec/into functions.
options = step: 'general.Alta Personal' catalogName : 'Personal' api: 'catalogs' protocol: 'http' exec g4catalogspublishAndAffect optionsinto errresultreturn callback errresult# returns: 1 or Error
This functions requires an outgoing (al Kernel) mapping:
Currency
priceNet(options)
options
Type: Object
Calculates a net price, passing an object with the following keys:
Price
Money NumberWithTaxes
Boolean which indicates if the netPrice has tax includedDiscount
Number for discount in line (percent or direct cost)DiscountInPercent
Boolean which indicates if the line discount is in Number or % formatGlobalDiscount
Number for 2nd discountTax1
Number for tax (e.g. IVA)Tax2
Number for tax 2 (e.g. IEPS)
Example:
options = Price: @Precio WithTaxes: g4containsExtraConImpuestos'si' Discount: @Descuento DiscountInPercent: g4containsExtraDescuentoEnPorcentaje'si' GlobalDiscount: ExtraDescuentoGlobal Tax1: @IVA Tax2: @IEPS result g4priceNetoptions
Depends on:
- [contains]
[contains]: https://github.com/IntelisisG4/documentacion/blob/master/expressions %26 syntax/strings.md#g4contains)]
dailyWage(wage)
wage
Type: Number
Calculates daily wage.
wage
indicates quantity of wage per month to calculate
Example:
# Example in coffee wage = 3000dailyWage = g4dailyWage wage consolelog dailyWage # 100
Date
dateAdd(datePart, number, date)
datePart
Type: String
number
Type: Number
date
Type: Number
Modify the original date by adding time.
datePart
time measurementnumber
indicates number to addingdate
indicates date to modify in format iso date eg: 2016-01-01T12:00:00.000Z
Example:
# Example in coffee date = '2016-01-01T12:00:00.000Z'number = 4datePart = 'h' date = g4dateAdd datePartnumberdateconsolelog date # 2016-01-01T16:00:00.000Z
There are all the time measurements to use:
Key | Shorthand |
---|---|
years | y |
quarters | Q |
months | M |
weeks | w |
days | d |
hours | h |
minutes | m |
seconds | s |
milliseconds | ms |
If you want to add multiple different keys at the same time, you can pass them in as an object literal.
# Example in coffee date = '2016-01-01T12:00:00.000Z' date = g4dateAdd days:1hours: 4 dateconsolelog date # 2016-01-02T16:00:00.000Z
dateDiff(datePart, endDate, startDate)
datePart
Type: String
endDate
Type: String
startDate
Type: String
Gets the difference between two dates.
datePart
time measurementendDate
indicates first date to comparestartDate
indicates second date to compare
Example:
# Example in coffee datePart = 'y'startDate = '1991-02-24T03:45:56.334Z'endDate = '2016-03-08T18:45:56.334Z' difference = g4dateDiff datePartendDatestartDateconsolelog difference # 25
Tests
How to run the unit tests
> npm install> grunt test
License
Copyright (C) 2016 Intelisis Software, S.A. de C.V. - All Rights Reserved
Unauthorized copying or distributing of this repository, project or any part of it, via any medium is strictly prohibited
Proprietary and confidential