Lodash in Service Portal

lodash in service portal

Lodash in Service Portal

There are a number of client-side libraries included in Service Portal. We will be going over some of them in this next series, starting with the Lodash library.

The version of Lodash that is included in the Service Portal is 4.17.11. Please keep in mind that Lodash is only available client-side, so you can use it in your Client Script, but not in your Server Script.

About Lodash

Lodash is a JavaScript library that helps programmers write more concise and maintainable JavaScript and contains tools to simplify programming with strings, numbers, arrays, functions, and objects.

You can find the full documentation here: https://lodash.com/docs

Lodash can be broken down into several main areas:

  • Utilities – for simplifying common programming tasks such as determining type as well as simplifying math operations.
  • Function – simplifying binding, decorating, constraining, throttling, debouncing, currying, and changing the pointer.
  • String – conversion functions for performing basic string operations, such as trimming, converting to uppercase, camel case, etc.
  • Array – creating, splitting, combining, modifying, and compressing
  • Collection – iterating, sorting, filtering, splitting, and building
  • Object – accessing, extending, merging, defaults, and transforming
  • Seq – chaining, wrapping, filtering, and testing.

Some examples

Lodash contains hundreds of helper functions. Although I can’t go through all of them here, I have put together some common examples.

OBJECTS

_.clone(value)

Creates a shallow clone of value.

_.assign(object, [sources])

Assigns own enumerable string keyed properties of source objects to the destination object. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

ARRAYS

_.times(n, [iteratee=_.identity])

Invokes the iteratee n times, returning an array of the results of each invocation. The iteratee is invoked with one argument; (index).

_.uniq(array)

Creates a duplicate-free version of an array, using SameValueZero for equality comparisons, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.

FUNCTIONS

_.bind(func, thisArg, [partials])

COLLECTION

_.includes(collection, value, [fromIndex=0])

Checks if value is in collection. If collection is a string, it’s checked for a substring of value, otherwise, SameValueZero is used for equality comparisons. If fromIndex is negative, it’s used as the offset from the end of collection.

_.filter(collection, [predicate=_.identity])

Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

_.find(collection, [predicate=_.identity], [fromIndex=0])

Iterates over elements of collection, returning the first element predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

SEQ

_.chain(value)

Creates a Lodash wrapper instance that wraps value with explicit method chain sequences enabled. The result of such sequences must be unwrapped with _#value.

_.sortBy(collection, [iteratees=[_.identity]])

Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru each iteratee. This method performs a stable sort, that is, it preserves the original sort order of equal elements. The iteratees are invoked with one argument: (value).

_.map(collection, [iteratee=_.identity])

Creates an array of values by running each element in collection thru iteratee. The iteratee is invoked with three arguments: (value, index|key, collection).

Conclusion

Although some Lodash methods are becoming native JavaScript functions with ES6, there are still many useful methods that complement functional programming in Service Portal.

For more on Lodash, check out the website: https://lodash.com

For a Lodash cheatsheet, check out: https://devhints.io/lodash

What other Lodash methods do you find useful in Service Portal? Let me know in the comments.

5 comments

  1. Hi Nathan thanks so much this is quite useful to know that we can those kind of libs using now in Service Portal.
    greetings from munich

  2. I’ve been using lodash functions on widgets for some time already. It helps a lot having lodash available on the server script but that requires adding the library as a Script Include. Do you recommend that?

  3. Hey Nathan,
    this is really awesom! Still, (I am a bit of stupid…) it would be wonderful if you can show a real SP example 🙂

    Thanks in advance!

Leave a Reply

Your email address will not be published. Required fields are marked *