npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sfid

v1.1.0

Published

Work with Salesforce IDs, convert to/from long or short ids, guess type, and more.

Downloads

761

Readme

Node module to work with Salesforce Id

sfid

This module enables you to easily work with Salesforce Ids. You can convert from short to long version of the Id, guess object type, and more. Example:

var sfid = require('sfid');

console.log(sfid('80120000000test'));           // 80120000000testAAA
console.log(sfid.short('80120000000testAAA'));  // 80120000000test

Content

Install

npm install --save sfid

Command line usage

Here's some examples (sfid --help for more commands):

sfid 80120000000test             # 80120000000testAAA
sfid --short 80120000000testAAA  # 80120000000test
sfid --type 80120000000testAAA   # Order

You can also use to pipe files and normalize Ids:

sfid --cat < data.csv > data-fixed.csv

Normalizing Ids

Salesforce Ids have two versions (15-character and 18-character). The long version includes a 3-character "checksum", which encodes the upper/lower case characters. This enables you to export Ids in systems which doesn't take case into account. If you're working with upper-case or lower-case Ids, you can use this module to normalize back to proper Ids. Example:

console.log(sfid('80120000000TESTAAA'));  // 80120000000testAAA
console.log(sfid('80120000000TESTAAE'));  // 80120000000tEstAAE

Remember if you cut of the checksum and work with 15-character version (short Id), the recovery of upper-case and lower-case characters is not possible.

Guessing type

The type guessing is based on the prefix or the Id. The list is maintained in the prefixes.json file and originally curated from resources found online (a) (b). The information is provided as-is and is not 100% certain. Furthermore the function only tries to guess the type.

Example:

console.log(sfid.guessType('80120000000testAAA'));  // Order
console.log(sfid.guessType('00120000000testAAA'));  // Account

If you work with custom types, you can register them to be recognized:

sfid.registerPrefix('a02', 'MyCustomObject__c');
console.log(sfid.guessType('a0220000000testAAA'));  // MyCustomObject__c

For other custom objects, the function returns 'custom' and for unknown types 'unknown'. For invalid Ids the function returns an empty string ''.

Advanced example: Finding Ids in text

var regexp = sfid.regexp();
var text   = '[log][info] 2017-02-03T07:14:55.234Z - Conversion complete - account: 00120000000testAAA - contact: 00320000000testAAA - not an id: 80120000000testXYZ';
var match;

while ((match = regexp.exec(text)) !== null) {
    if (!sfid.isValid(match[0])) { continue; }

    console.log('Found %s Id at position %s: %s', sfid.guessType(match[0]), match.index, match[0]);
}

API

.long(id : String) -> String

Alias: sfid(id)

Converts to long Id (18-character). For 15-character Ids, it appends the checksum. For 18-character Ids, it normalizes the upper-case and lower-case characters. For invalid Ids, returns empty string ''.

.short(id : String) -> String

First normalize using sfid.long function and then cut of the checksum. For invalid Ids, returns empty string ''.

.isValid(id : String) -> Boolean

Checks if a value is a valid Id.

.guessType(id : String) -> String | Array<String>

Guess object type based on the Id prefix. For invalid Ids, returns empty string ''.

.registerPrefix(prefix : String, objectName : String) -> Void

Adds a 3-character prefix to recognize using sfid.guessType function. Can be used for custom objects.

.checksum(id : String) -> String

Calculates 3-character checksum to append for an Id. Works for both 15- and 18-character Ids. The function assumes input is at least 15 characters long.

.regexp(completeMatch : Boolean) -> Regexp

Return regexp to use for searching for Ids in a text (default), or match completely (if completeMatch parameter is true).

License

Code is licensed under MIT, please see license.md file for details.