Custom functions
It is possible to create customized functions using JavaScript. These functions can be created from two places:
-
From Project: The functions created from this page can be used in any model that belongs to the project.
-
From the Anonymize or Synthesize operations: Functions created from here only apply only to the selected column. These functions are not saved globally and thus they cannot be reused from other places in the application.
Creating a new function
The page for creating a new function is divided into 3 parts:
- Console: the area where the user codes the function.
- Preview: The user can see the result of the code being run by using the
Input
fields and clicking theRun
button. It is also possible to add more records to the preview. - Functions: A list of preset functions. When a function is clicked, a hint code of the function is pasted in the console.
Use $val
variable to call to your input values
Functions
chance (seed?)
Return a Chance instance
- seed: inital value to instantiate Chance with.
Examples
Check the [Chance package documentation ↗] (https://chancejs.com/) for more info
dateFormat (val, format)
Format date with a mask
- val: date value that can be a string, a number or a Date object.
- format: specifies the format in which the date will be shown.
# | input | output | |
---|---|---|---|
1 | 99/10/31 | -> | 1999-10-31 |
2 | 95-05-18 | -> | 1995-05-18 |
Examples
db
Connects to a database using Gigantics drivers. List of methods available:
connect (driver, options)
Return a connection to a database.
- driver: driver string. It can be
mysql
,postgres
,mssql
,oracle
,mongodb
orsqlite3
- options: list of options to establish the connection.
- host: endpoint address.
- port: port number.
- user: database user.
- password: database password for the specified user.
- db: database name.
Examples
decrypt (val, secret, options)
Decrypt data using aes-256-gcm algorithm
- val: string to be decrypted.
- secret: key to use to decrypt the string.
- options:
- saltLength: length of the salt parameter
deepReplace (oldStr, newStr, options, record)
Replace the same data everywhere in the current record
- oldStr: old string value.
- newStr: new strig to replace with.
- options:
- smartCase: if
true
word casing will be ignored when matching the string to be replaced.
- smartCase: if
- record: a record whose properties will be checked for data replacement.
Examples
encrypt (val, secret, options)
Encrypt data with aes-256-gcm
- val: string to be encrypted.
- secret: key to use to encrypt the string.
- options:
- saltLength: length of the salt parameter
faker
Return a Faker instance
Examples
Check the Faker package documentation for a complete list of available functions
file
Return a file object with the following methods available:
open (path, flags, ...args)
Open and possibly create a file.
- path: A path to a file. If a URL is provided, it must use the
file:
protocol. - flags: A string indicating the type of operations to be performed on the
returned file descriptor. Its values can be
r
,r+
,rs+
,w
,wx
,w+
,wx+
,a
,ax
,a+
orax+
- args:
- mode: A file mode. If a string is passed, it is parsed as an octal
integer. If not supplied, it defaults to
0o666
.
- mode: A file mode. If a string is passed, it is parsed as an octal
integer. If not supplied, it defaults to
write (file, data, options)
Asynchronously write data to a file, replacing the file if it already exists. It is unsafe to call fsPromises.writeFile() multiple times on the same file without waiting for the Promise to be resolved (or rejected).
- file: A path to a file.
- data: The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
- options: Either the encoding for the file, or an object optionally
specifying the encoding, file mode, and flag. If encoding is not supplied, the
default of
utf8
is used. If mode is not supplied, the default of0o666
is used. If mode is a string, it is parsed as an octal integer. If flag is not supplied, the default ofw
is used.
read (file, data, options)
Asynchronously read the entire contents of a file.
- file: A path to a file.
- options: An object that may contain an optional flag. If a flag is not provided, it defaults to 'r'.
append (file, data, options)
Asynchronously append data to a file, creating the file if it does not exist.
- file: A path to a file. If a URL is provided, it must use the
file:
protocol. URL support is experimental. If a FileHandle is provided, the underlying file will not be closed automatically. - data: The data to write. If something other than a
Buffer
orUint8Array
is provided, the value is coerced to a string. - options: Either the encoding for the file, or an object optionally
specifying the encoding, file mode, and flag. If encoding is not supplied, the
default of
utf8
is used. If mode is not supplied, the default of0o666
is used. If mode is a string, it is parsed as an octal integer. If flag is not supplied, the default ofa
is used.
readdir (path, ...args)
Asynchronously read a directory.
- path: A path to a file. If a URL is provided, it must use the file: protocol.
- options: The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used.
rename (oldPath, newPath)
Asynchronously change the name or location of a file or directory.
- oldPath: The path of the file aimed to be modified.
- newPath: New name or path for the file.
copyFile (src, dest, ...args)
Asynchronously copie src
to dest
. By default, dest
is overwritten if it
already exists. Node.js makes no guarantees about the atomicity of the copy
operation. If an error occurs after the destination file has been opened for
writing, Node.js will attempt to remove the destination.
- src: A path to the source file.
- dest: A path to the destination file.
- args:
- flags: An optional integer that specifies the behavior of the copy operation. The only supported flag is fs.constants.COPYFILE_EXCL, which causes the copy operation to fail if dest already exists.
truncate (path, ...args)
Asynchronously truncate a file to a specified length.
- path: A path to a file. If a URL is provided, it must use the file: protocol.
- args:
- len: If not specified, defaults to 0.
mkdir (path, ...args)
Asynchronously create a directory.
- path: A path to a file. If a URL is provided, it must use the file: protocol.
- args:
- options: Either the file mode, or an object optionally specifying the file mode and whether parent folders should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to 0o777.
mkdtemp (prefix, ...args)
Asynchronously create a unique temporary directory. Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
- prefix: string that will preceed the generated temporary directory.
- args:
- options: The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, 'utf8' is used.
base (path?)
Return base path
- path: File path to append to the base path.
home (path?)
Return home path
- path: File path to append to the home path.
Examples
genLabel (label, locale, opts)
Return generated data from a label
- label: Gigantics label name.
- locale: Locale string.
- opts:
- prefix: String to add before the fake data.
- suffix: String to add after the fake data.
- case: It can be
upper
,lower
,title
,kebab
,snake
. - suffixEnabled: Boolean that indicates whether suffix use is enabled.
- prefixEnabled: Boolean that indicates whether prefix use is enabled.
Examples
genLike (val, opts?)
Output random data that looks like the input
- val: String that will be used as reference for data generation.
- opts:
- alphabet: String containing the characters that will be used in the resulting data.
- re: Regular expression.
- size: Size of the resulting string.
- remove: String containing the characters to be removed.
- add: String that will be added to the data.
genRe (re, opts?)
Return random data that matches the input regexp
- re: Regular expression.
- opts:
- alphabet: String containing the characters that will be used in the resulting data.
- re: Regular expression.
- size: Size of the resulting string.
- remove: String containing the characters to be removed.
- add: String that will be added to the data.
Examples
log
Send messages to the job logger. List of methods available
info (msg, ...args)
debug (msg, ...args)
trace (lev, msg, ...args)
warn (msg, ...args)
error (msg, ...args)
fail (msg, ...args)
Examples
md5 (str, encoding)
Return an md5 hashed value
- str: String to be hashed.
- encoding: If not specified, its value will be
hex
.
mem
Reuse transformations values across entities and fields. List of methods available:
do (scope, oldVal, fn, options?)
Store a memory entry by applying a certain transformation if the value is not set already. If it was previously stored, it returns the memoized value.
- scope: The context or scope the value is or will be stored under.
- oldVal: Key value to look for in memory.
- fn: Transformation to be stored.
- options:
- once: If
true
, the value won't be stored or updated when it previously existed. Its value isfalse
by default.
- once: If
set (scope, oldVal, newVal, options)
Store a value in memory under a certain scope.
- scope: The context or scope the value will be stored under.
- oldVal: Key value to look up the memory entry.
- newVal: The new value to be stored.
- options:
- once: If true, the value won't be stored or updated if it exists.
get (scope, oldVal)
Retrieve a value from memory.
- scope: The context or scope the value is stored under.
- oldVal: Key value to look up the memory entry.
Examples
Help
- Use the
mem
package if you need to sincronize data across different columns/fields and entity tables. - The mem package stores and retrieves data from project-wide dictionary, and sets values using a combination of a scope and a hashed or encrypted field value.
- We recommend the use of the
mem.do
method. It will only set a value if the value is not found, and return either the memoized value, if it exists, or the function return value. mem.get(scope, $val)
- returns the memoized value, orundefined
if not found.mem.set(scope, $val, newVal)
- sets a new value into a scope
once (cb)
Run a function once, caches and returns the result.
- cb: Function to be run
randomDate (start?, end?)
Return a random date, between two given dates.
- start: Start date as number, string or Date object.
- end: End date as number, string or Date object.
randomNumber (min, max, type, opts)
Return random number of the specified type between two values
- min: lower value the generated number can take
- max: higher value the generated number can take
- type: Can be
integer
orfloating
- opts:
- fixed: Specifies the number of decimals when the type is
floating
- fixed: Specifies the number of decimals when the type is
sha (val, algo, inFmt, outFmt)
Return a hashed value from input
- val: Value to be hashed.
- algo: Algorithm to be applied. It can take one of the following values:
SHA-1
,SHA-224
,SHA3-224
,SHA-256
,SHA3-256
,SHA-384
,SHA3-384
,SHA-512
,SHA3-512
,SHAKE128
, orSHAKE256
- intFmt: Input format. It can take one of the following values:
HEX
,TEXT
,B64
,BYTES
,ARRAYBUFFER
, orUINT8ARRAY
- outFmt: Output format. It can take one of the following values
B64
,HEX
,BYTES
,ARRAYBUFFER
, orUINT8ARRAY
smartReplace (str, before, after)
Replace a string keeping the casing
- str: Source string.
- before: String to match.
- after: String to use as a replacement.
strReverse (str)
Reverse a given string.
- str: String to reverse
# | input | output | |
---|---|---|---|
1 | foo | -> | oof |
2 | bar | -> | rab |
Examples
We can create a function to convert a temperature column from Farenheit to Celsius:
Where $val
represents the value of the field in the table.
Input -> Output:
# | input | output | |
---|---|---|---|
1 | 85 | -> | 29.444444444444446 |
2 | 50 | -> | 10 |
We can also use JavaScript functions:
Input -> Output:
# | input | output | |
---|---|---|---|
1 | 85 | -> | 29.44 |
2 | 50 | -> | 10.00 |