cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Expression Library Starter Pack

koryknick
Employee
Employee

Download Link
Shared Libraries.zip (4.8 KB)

Description:
These expression files have been created as examples for integration professionals and/or convenience functions for citizen developers. The expression files can be included at the organizationโ€™s root-level shared folder or project-space shared folder for ease of use. The included pipeline provides simple examples for many of the functions contained in these libraries.

Samples:
lib.string.lpad(โ€˜123โ€™, 5, โ€˜0โ€™) yields โ€œ00123โ€
lib.date.fromEpoch(1234567890) yields โ€œ2009-02-13T23:31:30.000 UTCโ€
lib.date.trunc(Date.now(), โ€˜HHโ€™) yields current date/time truncated to hour

Release used: master-7317 - 4.21 GA

=============================================================

date.expr

Many new customers struggle with date manipulation in JavaScript. This expression library was created to provide some of the functionality they may see as โ€œmissingโ€ in JavaScript.

Static Variables

Two static variables are defined:

  • timezone - The timezone to be used by the functions in this expression library if a timezone is not provided. This should be set using the appropriate timezone values for Date.toLocaleDateTimeString function
  • defaultFormat - The format string to be used by the functions in this expression library if a format string is not provided

Functions

  • format (Date[, FormatString[, TimeZone]]) - format a Date object, returned as a String
    • If FormatString is not specified, โ€œdefaultFormatโ€ provided in the Static Variables section will be used
    • If TimeZone is not specified, โ€œtimezoneโ€ provided in the Static Variables section will be used
  • firstDayOfMonth (Date) - return a Date object for the first day of the month from the Date object specified
  • lastDayOfMonth (Date) - return a Date object for the last day of the month from the Date object specified
  • toEpoch (Date) - return a BigInt value representing the epoch time (number of seconds since 1-Jan-1970) from the Date object specified
  • fromEpoch (seconds) - return a Date object calculated from the epoch time specified
  • getAge (Date, AsOfDate) - return an integer value representing the difference in years between AsOfDate and Date specified
    • If AsOfDate is not specified, current date/time is used
  • segmentHour (Date, numSegments) - return a Date object where the Minutes value has been set according to the number of segments specified.
    • For example, if numSegments is 4 the minutes returned will be 00, 15, 30, 45; if numSegments is 6 the minutes returned will be 00, 10, 20, 30, 40, 50
    • Note that the minutes value is truncated (not rounded) to the segment, so a numSegments of 4:
      • Minutes 0-14 โ†’ 00
      • Minutes 15-29 โ†’ 15
      • Minutes 30-44 โ†’ 30
      • Minutes 45-59 โ†’ 45
  • trunc (Date[, FormatString) - return a Date object that has been truncated based on the desired format
    • Valid FormatString values:
      • yyyy - truncate to year
      • MM - truncate to month
      • dd - truncate to day
      • HH - truncate to hour
      • mm - truncate to minute
      • ss - truncate to second

string.expr

Many new customers struggle with formatting strings for specific purposes. This expression library was created to provide some of the functionality they may see as โ€œmissingโ€ in JavaScript.

Functions

  • lpad (String, size[, fill-character]) - return a String value that has been left-padded to the specified size, using the fill-character specified
    • Default fill character is space
    • String will be truncated to โ€œsizeโ€ if original string is longer
  • rpad (String, size[, fill-character]) - return a String value that has been right-padded to the specified size, using the fill-character specified
    • Default fill character is space
    • String will be truncated to โ€œsizeโ€ if original string is longer
  • objectToKeyValuePairs (Object[, key/value separator[, data separator[, encode]]]) - return a String that contains key/value pairs joined from all fields in an object
    • If โ€œkey/value separatorโ€ is not specified, default value colon โ€œ:โ€ is used
    • If โ€œdata separatorโ€ is not specified, default value comma โ€œ,โ€ is used
    • If โ€œencodeโ€ is set to true, each data value will be passed through encodeURIComponent
  • keyValuePairsToObject (String[, key/value separator[, data separator[, decode]]]) - return an Object parsed from the key/value pairs String
    • If โ€œkey/value separatorโ€ is not specified, default value colon โ€œ:โ€ is used
    • If โ€œdata separatorโ€ is not specified, default value comma โ€œ,โ€ is used
    • If โ€œdecodeโ€ is set to true, each data value will be passed through decodeURIComponent
  • flattenObject (Object, SubObjectName) - return a new object with the members of SubObjectName merged into Object; useful for flattening data after a Lookup or other snaps that move data into โ€œoriginalโ€
  • numberWithCommas (Number[, Precision]) - return a String that is formatted from the specified Number, placing a comma โ€œ,โ€ character at every 3rd position
    • Decimal values are not formatted but will be rounded to the requested precision
  • bytesToFileSize (Bytes[, Precision]) - convert a number that represents a file size in bytes to a human readable (concise) format up to YottaByte notation

xml.expr

The XML Generator does not handle incoming data that is not XML-safe. This expression library provides simple functions to encode and decode String values.

Functions

  • encode (String) - return a String that has replaced all occurrences of XML special characters with appropriate escape-sequence strings
  • decode (String) - return a String that has replaced all occurrences of XML escape-string sequences with equivalent symbols
5 REPLIES 5

alchemiz
Contributor III

Nice! I do have a similar extender functions like lpad and rpad but uses the slice string method for lpad ๐Ÿ™‚

'lpad' : (source, totalchar, padchar) => ((padchar.repeat(totalchar) + source).slice((totalchar * -1))),
'rpad' : (source, totalchar, padchar) => ((source + padchar.repeat(totalchar)).substr(0,totalchar))

Also with the XML encode there was a previous post here wherein you will only call the replace method one time

image

Thank you for bringing that article to my attention @alchemiz! The xml.expr is already obsolete! SnapLogic has HTML.encode() and decode() functions that do the same thing.

I have updated the shared library to remove the xml.expr in favor of the HTML functions.

Shared Libraries.zip (4.6 KB)

deepanshu_1
New Contributor III

Dear All,

I am receiving error when I am importing the pipeline, can some one help here:
image

Thanks in advance,
Deepanshu

Hi @deepanshu.1 - The expr files can be added in Manager / Files. If you still get an error, please ensure you have write permissions to the project.