g4-functions v1.0.8
G4 functions module
Module of custom functions
Table of Contents
Prerequisites
Installation
Package installation
> npm install @intelisis/g4-functions --save
Browser include
<script src="/node_modules/@intelisis/g4-functions/browser/index.js"></script>
Usage
Initialize
To initialize a new navigation region:
// JS Example
const g4 = g4
# Coffee Example
g4 = g4
String funcions
upperCase(stringToUppercase)
stringToUppercase
Type: String
Changes the case of the whole string.
result g4.upperCase('intelisis')
# returns: INTELISIS
left(string, numberCharacters)
string
Type: String
numberCharacters
Type: Number
- characters defaults to: 1
Returns the first n characters of String
result g4.left('Automoriz', 3)
# returns: Aut
result g4.left('F1238432')
# returns: F
right(string, numberCharacters)
string
Type: String
numberCharacters
Type: Number
- characters defaults to: 1
Returns the last n characters of String
result g4.right('Intelisis', 3)
# returns: sis
result g4.right('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 g4.contains(haystack, needle)
# returns: true
# search string inside array object's values
needle = 'Intelisis'
haystack = [
name: 'Intelisis is cool'
,
name: 'Apple is cooler'
]
result g4.contains(haystack, needle)
# returns: true
# can't find a string inside an array
needle = 'G4'
haystack = ['foo', 'Intelisis', 'bar']
result g4.contains(haystack, needle)
# 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.
result g4.sum([1,5,7,2])
# returns: 15
result g4.sum(2, 1, 4, 3)
# 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 g4.round(5.5234)
# returns: 5.52
result g4.round(5.5234, 3)
# returns: 5.523
floor(NumberToFloor)
NumberToFloor
Type: Number
Floors a Number to the ground
result g4.floor(123.45)
# returns: 123
result g4.floor(-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 g4.getNumericPercentage(IVA)
# returns: 0
Iva = '16%'
result g4.getNumericPercentage(IVA)
# 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
g4.copyToColumn(DatosGenerales.Folio, Detalle, '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 g4.findRow(list, '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 g4.getColumn(list, '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 = [1, 4, 9]
result g4.map(list, (num) -> 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 = [1, 2, 3, 4, 5, 6]
result g4.reject(list, (num) -> 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 = [1, 2, 3, 4, 5, 6]
result g4.filter(list, (num) -> 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 = [1, 2, 3, 4, 5, 6]
result g4.find(list, (num) -> 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 = [1, 2, 3]
result g4.reduce(list, (memo, num) -> 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 g4.deep(obj, '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 g4.documents().updateSection options, into err, result
return callback err, result
# 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 g4.catalogs().publishAndAffect options, into err, result
return callback err, result
# 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: g4.contains(Extra.ConImpuestos, 'si')
Discount: @Descuento
DiscountInPercent: g4.contains(Extra.DescuentoEnPorcentaje, 'si')
GlobalDiscount: Extra.DescuentoGlobal
Tax1: @IVA
Tax2: @IEPS
}
result g4.priceNet(options)
Depends on:
contains: https://github.com/IntelisisG4/documentacion/blob/master/expressions%20%26%20syntax/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 = 3000
dailyWage = g4.dailyWage wage
console.log 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 = 4
datePart = 'h'
date = g4.dateAdd datePart, number, date
console.log 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 = g4.dateAdd { days:1, hours: 4 }, date
console.log 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 = g4.dateDiff datePart, endDate, startDate
console.log 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
7 years ago