Giter VIP home page Giter VIP logo

glider.js's Introduction

Like Lodash for GlideRecord

NOTE: This library is currently experimental, use in production with caution

Glider.js is a utility library for Javascript, similar to Lodash and Underscore, that is specifically designed to work with GlideRecords (but will also work with Arrays and other collections) on the ServiceNow platform. Glider is largely inspired by Lazy.js and uses lazy evaluation to improve loop performance and ensure consistency with GlideRecords.

To get started, you should install Glider.js as a Script Include. The following options are available:

Global Scope Script Include

Grab the Glider Update Set.xml Update Set under the dist folder and install it in your ServiceNow instance to create the Glider Script Include in the global scope.

Scoped Application Script Include

Create a new Script Include called "Glider" in your ServiceNow Scoped Application. Paste the contents of the Glider.js or the Glider.min.js file directly into the Script Include script field and save the record.

Introduction

If you are familiar with Lodash syntax, Glider will be quite familiar but for the uninitiated let's take a look at the advantages of the Glider syntax:

Glider('incident', 'active=true')
  .filter(function(gr) { return gr.canRead(); })
  .map(function(gr) {
    return {
      'short_description': gr.short_description + ''
    };
  })
  .value();

The equivalent vanilla GlideRecord loop approach would look something like this:

var gr = new GlideRecord('incident'),
  arr = [],
  obj;

gr.addEncodedQuery('active=true');
gr.query()

while(gr.next()) {  
  if (gr.canRead()) {
    obj = {
      'short_description': gr.short_description + ';'
    };

    arr.push(obj);
  }
}

You'll note that Glider gets rid of the GlideRecord boilerplate and global variables. But we can actually clean up Glider even more by extracting some of those callbacks:

function canRead(gr) {
  return gr.canRead();
}

function buildIncidentObj(gr) {
  return {
    'short_description': gr.short_description + '';
  }
}

Glider('incident', 'active=true')
  .filter(canRead)
  .map(buildIncidentObject)
  .value();

By extracting the callback functions, we can easily read the entire collection pipeline and understand its process. Moving the callbacks into their own Script Includes (such as canRead) additionally make those calls reusable in other Glider scripts in the instance.

It's much more complicated to yield these same improvements on vanilla GlideRecord loops, especially as the nested depth of loops and if statements increase. Additionally, extraction can have unintended consequences on while loop flow control since statements such as break and continue have no meaning once removed from the loop.

Features

GlideRecordSequence Iteration

Glider('incident', 'active=true')
  .map(function(gr) { return gr.short_description + '';})
  .value();
// returns an array of short description strings such as ['test', 'my computer is broken', 'etc']

ArraySequence Iteration

Glider([1,2,3,4,5,6])
  .map(function(el) { return el + 1; })
  .value();
// returns an array [2,3,4,5,6,7]

Pipeline Functions

Pipeline functions return Sequence objects which can be chained together in order to lazily process the items in the collection passed to Glider.

  • .map(callback) - Generates a sequence by executing the callback on each item and yielding the returned value
  • .filter(callback) - Generates a sequence by executing the callback on each item and yielding only values where the callback returns true
  • .chunk(size) - Breaks the sequence into chunks of the specified size by yielding one ChunkItemSequence at a time. The ChunkItemSequence can be iterated using Glider functions to yield the items in the chunk.

Terminal Functions

Terminal functions end a Sequence, execute the Pipeline functions, and return a value.

  • .value() - Returns an array of processed values
  • .forEach(callback) - Iterates the sequences, passing each processes value to the callback
  • .reduce(callback, initial) - Reduces the iterated sequence of values by passing each value to the callback (using an optional initial value) and returning a single accumulated value

REMINDER: This library is currently experimental. Expect bugs and failures at this point.

glider.js's People

Contributors

tltoulson avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.