All Projects → tltoulson → Glider.js

tltoulson / Glider.js

Licence: MIT license
A lodash style library for ServiceNow GlideRecord

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to Glider.js

UXsyncNow
UXstorm's ServiceNow file synchronizer / development tool
Stars: ✭ 23 (+21.05%)
Mutual labels:  servicenow
servicenow-powershell
PowerShell module to automate ServiceNow service and asset management. This module can be used standalone, with Azure Automation, or Docker.
Stars: ✭ 310 (+1531.58%)
Mutual labels:  servicenow
servicenow-instance-wakeup
Wake up your servicenow instance from hibernation
Stars: ✭ 35 (+84.21%)
Mutual labels:  servicenow
code-snippets
ServiceNow's Code Snippets community repository, managed by the Developer Program and the sndevs Slack community.
Stars: ✭ 66 (+247.37%)
Mutual labels:  servicenow
terrasnow-enterprise
Terrraform resource management from ServiceNow
Stars: ✭ 16 (-15.79%)
Mutual labels:  servicenow
SpoketoberfestResources
No description or website provided.
Stars: ✭ 16 (-15.79%)
Mutual labels:  servicenow
servicenow
A golang client for ServiceNow
Stars: ✭ 16 (-15.79%)
Mutual labels:  servicenow
ServiceNow-GraphQL-Example
Simple example how to use GraphQL in the latest ServiceNow Release: Paris
Stars: ✭ 18 (-5.26%)
Mutual labels:  servicenow
django-snow
ServiceNow Ticket Management App for Django based projects
Stars: ✭ 16 (-15.79%)
Mutual labels:  servicenow
ticketutil
Python ticketing utility for working with tickets in popular tools
Stars: ✭ 58 (+205.26%)
Mutual labels:  servicenow
servicenow-bot-example
A ServiceNow Bot example for Slack
Stars: ✭ 35 (+84.21%)
Mutual labels:  servicenow
docker-mid-server
All versions of ServiceNow MID Server as Docker Container
Stars: ✭ 20 (+5.26%)
Mutual labels:  servicenow
terraform-provider-servicenow
Terraform provider to manage ServiceNow objects.
Stars: ✭ 25 (+31.58%)
Mutual labels:  servicenow

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].