@qeepsake/rails-guid
Advanced tools
Comparing version
@@ -8,6 +8,7 @@ /** | ||
* @param gid - gid://qeepsake-rails/Model/55587 | ||
* @param stripTimestamp - whether to strip the timestamp from the ID (i.e. %2C2022-09-26+13%3A08%3A27+UTC) | ||
* | ||
* @returns 55587 | ||
*/ | ||
export declare function extractRailsId(gid: string): string; | ||
export declare function extractRailsId(gid?: string | null): string | null; | ||
export declare function extractRailsId(gid: string, stripTimestamp?: boolean): string; | ||
export declare function extractRailsId(gid?: string | null, stripTimestamp?: boolean): string | null; |
@@ -1,5 +0,9 @@ | ||
export function extractRailsId(gid) { | ||
export function extractRailsId(gid, stripTimestamp = false) { | ||
if (typeof gid === 'string' && (gid === null || gid === void 0 ? void 0 : gid.indexOf('/')) != -1) { | ||
const gidComponents = gid.split('/'); | ||
const extractedId = gidComponents[gidComponents.length - 1]; | ||
let extractedId = gidComponents[gidComponents.length - 1]; | ||
// Strips the timestamp (%2C2022-09-26+13%3A08%3A27+UTC)) from the ID | ||
if (stripTimestamp && (extractedId === null || extractedId === void 0 ? void 0 : extractedId.includes('%'))) { | ||
extractedId = extractedId.split('%')[0]; | ||
} | ||
if (extractedId) { | ||
@@ -6,0 +10,0 @@ return extractedId; |
@@ -1,6 +0,16 @@ | ||
function extractRailsId(gid) { | ||
function extractRailsId(gid, stripTimestamp) { | ||
if (stripTimestamp === void 0) { | ||
stripTimestamp = false; | ||
} | ||
if (typeof gid === 'string' && (gid === null || gid === void 0 ? void 0 : gid.indexOf('/')) != -1) { | ||
var _extractedId; | ||
var gidComponents = gid.split('/'); | ||
var extractedId = gidComponents[gidComponents.length - 1]; | ||
if (stripTimestamp && (_extractedId = extractedId) !== null && _extractedId !== void 0 && _extractedId.includes('%')) { | ||
extractedId = extractedId.split('%')[0]; | ||
} | ||
if (extractedId) { | ||
@@ -7,0 +17,0 @@ return extractedId; |
@@ -1,6 +0,16 @@ | ||
function extractRailsId(gid) { | ||
function extractRailsId(gid, stripTimestamp) { | ||
if (stripTimestamp === void 0) { | ||
stripTimestamp = false; | ||
} | ||
if (typeof gid === 'string' && (gid === null || gid === void 0 ? void 0 : gid.indexOf('/')) != -1) { | ||
var _extractedId; | ||
var gidComponents = gid.split('/'); | ||
var extractedId = gidComponents[gidComponents.length - 1]; | ||
if (stripTimestamp && (_extractedId = extractedId) !== null && _extractedId !== void 0 && _extractedId.includes('%')) { | ||
extractedId = extractedId.split('%')[0]; | ||
} | ||
if (extractedId) { | ||
@@ -7,0 +17,0 @@ return extractedId; |
{ | ||
"name": "@qeepsake/rails-guid", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Tiny (zero dependancy) utility to create and extract rails guid strings in JavaScript 🛤", | ||
@@ -5,0 +5,0 @@ "author": "lukebrandonfarrell", |
@@ -8,12 +8,24 @@ /** | ||
* @param gid - gid://qeepsake-rails/Model/55587 | ||
* @param stripTimestamp - whether to strip the timestamp from the ID (i.e. %2C2022-09-26+13%3A08%3A27+UTC) | ||
* | ||
* @returns 55587 | ||
*/ | ||
export function extractRailsId(gid: string): string | ||
export function extractRailsId(gid?: string | null): string | null | ||
export function extractRailsId(gid?: string | null): string | null { | ||
export function extractRailsId(gid: string, stripTimestamp?: boolean): string | ||
export function extractRailsId( | ||
gid?: string | null, | ||
stripTimestamp?: boolean, | ||
): string | null | ||
export function extractRailsId( | ||
gid?: string | null, | ||
stripTimestamp = false, | ||
): string | null { | ||
if (typeof gid === 'string' && gid?.indexOf('/') != -1) { | ||
const gidComponents = gid.split('/') | ||
const extractedId = gidComponents[gidComponents.length - 1] | ||
let extractedId = gidComponents[gidComponents.length - 1] | ||
// Strips the timestamp (%2C2022-09-26+13%3A08%3A27+UTC)) from the ID | ||
if (stripTimestamp && extractedId?.includes('%')) { | ||
extractedId = extractedId.split('%')[0] | ||
} | ||
if (extractedId) { | ||
@@ -20,0 +32,0 @@ return extractedId |
@@ -13,2 +13,10 @@ const { extractRailsId } = require('../src/extract-rails-id'); | ||
expect(extractRailsId('The quick brown fox jumps over the lazy dog')).toBe(null); | ||
}); | ||
test('Extract a Rails GUID with a timestamp', () => { | ||
expect(extractRailsId('gid://qeepsake-rails/MyModel/12345%2C2022-09-26+13%3A08%3A27+UTC')).toBe('12345%2C2022-09-26+13%3A08%3A27+UTC'); | ||
}); | ||
test('Extract a Rails GUID and strip the timestamp', () => { | ||
expect(extractRailsId('gid://qeepsake-rails/MyModel/12345%2C2022-09-26+13%3A08%3A27+UTC', true)).toBe('12345'); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24479
13.85%337
11.96%