diff --git a/README.md b/README.md index 972666c..6eb3fde 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,15 @@ const id = 55587 || "55587"; createRailsId(id, "Model") // -> gid://qeepsake-rails/Model/55587 ``` +### Tetsing for Extracted Rails ID + +```js +import { isExtractedRailsId } from '@qeepsake/rails-guid'; + +isExtractedRailsId("55587") // => true +isExtractedRailsId("gid://qeepsake-rails/Model/55587") // => false +``` + ## License MIT © [lukebrandonfarrell](https://github.com/lukebrandonfarrell) diff --git a/lib/extract-rails-id.js b/lib/extract-rails-id.js index bc435a3..ac98cee 100644 --- a/lib/extract-rails-id.js +++ b/lib/extract-rails-id.js @@ -1,4 +1,8 @@ +import { isExtractedRailsId } from './is-extracted-rails-id'; export function extractRailsId(gid, stripTimestamp = false) { + // Checks if ID has already been extracted, if so, return it + if (gid && isExtractedRailsId(gid)) + return gid; if (typeof gid === 'string' && (gid === null || gid === void 0 ? void 0 : gid.indexOf('/')) != -1) { const gidComponents = gid.split('/'); let extractedId = gidComponents[gidComponents.length - 1]; diff --git a/lib/index.d.ts b/lib/index.d.ts index 15f59bd..3b11d69 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,2 +1,3 @@ export { extractRailsId } from './extract-rails-id'; export { createRailsId } from './create-rails-id'; +export { isExtractedRailsId } from './is-extracted-rails-id'; diff --git a/lib/index.js b/lib/index.js index 15f59bd..3b11d69 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,2 +1,3 @@ export { extractRailsId } from './extract-rails-id'; export { createRailsId } from './create-rails-id'; +export { isExtractedRailsId } from './is-extracted-rails-id'; diff --git a/lib/index.js.map b/lib/index.js.map index f2a6e25..57de660 100644 --- a/lib/index.js.map +++ b/lib/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../src/extract-rails-id.ts","../src/create-rails-id.ts"],"sourcesContent":["/**\n * Extract rails ID\n *\n * If the ID is passed in the format of gid://qeepsake-rails/JournalEntry/55587\n * then it will be returned 55587\n *\n * @param gid - gid://qeepsake-rails/Model/55587\n * @param stripTimestamp - whether to strip the timestamp from the ID (i.e. %2C2022-09-26+13%3A08%3A27+UTC)\n *\n * @returns 55587\n */\n export function extractRailsId(gid: string, stripTimestamp?: boolean): string;\n export function extractRailsId(gid?: string | null, stripTimestamp?: boolean): string | null;\n export function extractRailsId(gid?: string | null, stripTimestamp = false): string | null {\n if (typeof gid === 'string' && gid?.indexOf('/') != -1) {\n const gidComponents = gid.split('/')\n let extractedId = gidComponents[gidComponents.length - 1]\n\n // Strips the timestamp (%2C2022-09-26+13%3A08%3A27+UTC)) from the ID \n if(stripTimestamp && extractedId?.includes('%')) {\n extractedId = extractedId.split('%')[0];\n }\n\n if (extractedId) {\n return extractedId\n }\n }\n\n return null\n}\n","/**\n * Creates a rails id\n *\n * If the ID is passed in the format of 55587, then it will be returned\n * gid://qeepsake-rails/JournalEntry/55587\n *\n * @param id - 55587\n * @param model - JournalEntry\n *\n * @returns gid://qeepsake-rails/Model/55587\n */\nexport function createRailsId(id: number | string, model: string): string {\n return `gid://qeepsake-rails/${model}/${id}`\n}\n"],"names":["extractRailsId","gid","stripTimestamp","indexOf","gidComponents","split","extractedId","length","includes","createRailsId","id","model"],"mappings":"SAaiBA,eAAeC,KAAqBC;MAAAA;AAAAA,IAAAA,iBAAiB;;;AACpE,MAAI,OAAOD,GAAP,KAAe,QAAf,IAA2B,CAAAA,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEE,OAAL,CAAa,GAAb,MAAqB,CAAC,CAArD,EAAwD;AAAA;;AACtD,QAAMC,aAAa,GAAGH,GAAG,CAACI,KAAJ,CAAU,GAAV,CAAtB;AACA,QAAIC,WAAW,GAAGF,aAAa,CAACA,aAAa,CAACG,MAAd,GAAuB,CAAxB,CAA/B;;AAGC,QAAGL,cAAc,oBAAII,WAAJ,yCAAI,aAAaE,QAAb,CAAsB,GAAtB,CAArB,EAAiD;AAChDF,MAAAA,WAAW,GAAGA,WAAW,CAACD,KAAZ,CAAkB,GAAlB,EAAuB,CAAvB,CAAd;AACD;;AAED,QAAIC,WAAJ,EAAiB;AACf,aAAOA,WAAP;AACD;AACF;;AAED,SAAO,IAAP;AACD;;SClBeG,cAAcC,IAAqBC;AACjD,mCAA+BA,KAA/B,SAAwCD,EAAxC;AACD;;;;;"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../src/is-extracted-rails-id.ts","../src/extract-rails-id.ts","../src/create-rails-id.ts"],"sourcesContent":["\n/**\n * Checks to see if the given string is a valid extracted Rails ID\n * i.e. 55587 and not gid://qeepsake-rails/JournalEntry/55587\n * \n * @param id - rails ID\n * @returns if ID is extracted rails ID\n */\nexport function isExtractedRailsId(id: string): boolean {\n return !id?.startsWith('gid://');\n}","import { isExtractedRailsId } from \"./is-extracted-rails-id\"\n\n/**\n * Extract rails ID\n *\n * If the ID is passed in the format of gid://qeepsake-rails/JournalEntry/55587\n * then it will be returned 55587\n *\n * @param gid - gid://qeepsake-rails/Model/55587\n * @param stripTimestamp - whether to strip the timestamp from the ID (i.e. %2C2022-09-26+13%3A08%3A27+UTC)\n *\n * @returns 55587\n */\nexport function extractRailsId(gid: string, stripTimestamp?: boolean): string\nexport function extractRailsId(\n gid?: string | null,\n stripTimestamp?: boolean,\n): string | null\nexport function extractRailsId(\n gid?: string | null,\n stripTimestamp = false,\n): string | null {\n // Checks if ID has already been extracted, if so, return it\n if(gid && isExtractedRailsId(gid)) {\n return gid\n }\n\n if (typeof gid === 'string' && gid?.indexOf('/') != -1) {\n const gidComponents = gid.split('/')\n let extractedId = gidComponents[gidComponents.length - 1]\n\n // Strips the timestamp (%2C2022-09-26+13%3A08%3A27+UTC)) from the ID\n if (stripTimestamp && extractedId?.includes('%')) {\n extractedId = extractedId.split('%')[0]\n }\n\n if (extractedId) {\n return extractedId\n }\n }\n\n return null\n}\n","/**\n * Creates a rails id\n *\n * If the ID is passed in the format of 55587, then it will be returned\n * gid://qeepsake-rails/JournalEntry/55587\n *\n * @param id - 55587\n * @param model - JournalEntry\n *\n * @returns gid://qeepsake-rails/Model/55587\n */\nexport function createRailsId(id: number | string, model: string): string {\n return `gid://qeepsake-rails/${model}/${id}`\n}\n"],"names":["isExtractedRailsId","id","startsWith","extractRailsId","gid","stripTimestamp","indexOf","gidComponents","split","extractedId","length","includes","createRailsId","model"],"mappings":"SAQgBA,mBAAmBC;AACjC,SAAO,EAACA,EAAD,aAACA,EAAD,eAACA,EAAE,CAAEC,UAAJ,CAAe,QAAf,CAAD,CAAP;AACD;;SCQeC,eACdC,KACAC;MAAAA;AAAAA,IAAAA,iBAAiB;;;AAGjB,MAAGD,GAAG,IAAIJ,kBAAkB,CAACI,GAAD,CAA5B,EAAmC;AACjC,WAAOA,GAAP;AACD;;AAED,MAAI,OAAOA,GAAP,KAAe,QAAf,IAA2B,CAAAA,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEE,OAAL,CAAa,GAAb,MAAqB,CAAC,CAArD,EAAwD;AAAA;;AACtD,QAAMC,aAAa,GAAGH,GAAG,CAACI,KAAJ,CAAU,GAAV,CAAtB;AACA,QAAIC,WAAW,GAAGF,aAAa,CAACA,aAAa,CAACG,MAAd,GAAuB,CAAxB,CAA/B;;AAGA,QAAIL,cAAc,oBAAII,WAAJ,yCAAI,aAAaE,QAAb,CAAsB,GAAtB,CAAtB,EAAkD;AAChDF,MAAAA,WAAW,GAAGA,WAAW,CAACD,KAAZ,CAAkB,GAAlB,EAAuB,CAAvB,CAAd;AACD;;AAED,QAAIC,WAAJ,EAAiB;AACf,aAAOA,WAAP;AACD;AACF;;AAED,SAAO,IAAP;AACD;;SC/BeG,cAAcX,IAAqBY;AACjD,mCAA+BA,KAA/B,SAAwCZ,EAAxC;AACD;;;;;;"} \ No newline at end of file diff --git a/lib/index.modern.js b/lib/index.modern.js index 0d0f004..9ca83de 100644 --- a/lib/index.modern.js +++ b/lib/index.modern.js @@ -1,8 +1,16 @@ +function isExtractedRailsId(id) { + return !(id !== null && id !== void 0 && id.startsWith('gid://')); +} + function extractRailsId(gid, stripTimestamp) { if (stripTimestamp === void 0) { stripTimestamp = false; } + if (gid && isExtractedRailsId(gid)) { + return gid; + } + if (typeof gid === 'string' && (gid === null || gid === void 0 ? void 0 : gid.indexOf('/')) != -1) { var _extractedId; @@ -25,5 +33,5 @@ function createRailsId(id, model) { return "gid://qeepsake-rails/" + model + "/" + id; } -export { createRailsId, extractRailsId }; +export { createRailsId, extractRailsId, isExtractedRailsId }; //# sourceMappingURL=index.modern.js.map diff --git a/lib/index.modern.js.map b/lib/index.modern.js.map index de8b8a8..84cb0ba 100644 --- a/lib/index.modern.js.map +++ b/lib/index.modern.js.map @@ -1 +1 @@ -{"version":3,"file":"index.modern.js","sources":["../src/extract-rails-id.ts","../src/create-rails-id.ts"],"sourcesContent":["/**\n * Extract rails ID\n *\n * If the ID is passed in the format of gid://qeepsake-rails/JournalEntry/55587\n * then it will be returned 55587\n *\n * @param gid - gid://qeepsake-rails/Model/55587\n * @param stripTimestamp - whether to strip the timestamp from the ID (i.e. %2C2022-09-26+13%3A08%3A27+UTC)\n *\n * @returns 55587\n */\n export function extractRailsId(gid: string, stripTimestamp?: boolean): string;\n export function extractRailsId(gid?: string | null, stripTimestamp?: boolean): string | null;\n export function extractRailsId(gid?: string | null, stripTimestamp = false): string | null {\n if (typeof gid === 'string' && gid?.indexOf('/') != -1) {\n const gidComponents = gid.split('/')\n let extractedId = gidComponents[gidComponents.length - 1]\n\n // Strips the timestamp (%2C2022-09-26+13%3A08%3A27+UTC)) from the ID \n if(stripTimestamp && extractedId?.includes('%')) {\n extractedId = extractedId.split('%')[0];\n }\n\n if (extractedId) {\n return extractedId\n }\n }\n\n return null\n}\n","/**\n * Creates a rails id\n *\n * If the ID is passed in the format of 55587, then it will be returned\n * gid://qeepsake-rails/JournalEntry/55587\n *\n * @param id - 55587\n * @param model - JournalEntry\n *\n * @returns gid://qeepsake-rails/Model/55587\n */\nexport function createRailsId(id: number | string, model: string): string {\n return `gid://qeepsake-rails/${model}/${id}`\n}\n"],"names":["extractRailsId","gid","stripTimestamp","indexOf","gidComponents","split","extractedId","length","includes","createRailsId","id","model"],"mappings":"SAaiBA,eAAeC,KAAqBC;MAAAA;AAAAA,IAAAA,iBAAiB;;;AACpE,MAAI,OAAOD,GAAP,KAAe,QAAf,IAA2B,CAAAA,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEE,OAAL,CAAa,GAAb,MAAqB,CAAC,CAArD,EAAwD;AAAA;;AACtD,QAAMC,aAAa,GAAGH,GAAG,CAACI,KAAJ,CAAU,GAAV,CAAtB;AACA,QAAIC,WAAW,GAAGF,aAAa,CAACA,aAAa,CAACG,MAAd,GAAuB,CAAxB,CAA/B;;AAGC,QAAGL,cAAc,oBAAII,WAAJ,yCAAI,aAAaE,QAAb,CAAsB,GAAtB,CAArB,EAAiD;AAChDF,MAAAA,WAAW,GAAGA,WAAW,CAACD,KAAZ,CAAkB,GAAlB,EAAuB,CAAvB,CAAd;AACD;;AAED,QAAIC,WAAJ,EAAiB;AACf,aAAOA,WAAP;AACD;AACF;;AAED,SAAO,IAAP;AACD;;SClBeG,cAAcC,IAAqBC;AACjD,mCAA+BA,KAA/B,SAAwCD,EAAxC;AACD;;;;"} \ No newline at end of file +{"version":3,"file":"index.modern.js","sources":["../src/is-extracted-rails-id.ts","../src/extract-rails-id.ts","../src/create-rails-id.ts"],"sourcesContent":["\n/**\n * Checks to see if the given string is a valid extracted Rails ID\n * i.e. 55587 and not gid://qeepsake-rails/JournalEntry/55587\n * \n * @param id - rails ID\n * @returns if ID is extracted rails ID\n */\nexport function isExtractedRailsId(id: string): boolean {\n return !id?.startsWith('gid://');\n}","import { isExtractedRailsId } from \"./is-extracted-rails-id\"\n\n/**\n * Extract rails ID\n *\n * If the ID is passed in the format of gid://qeepsake-rails/JournalEntry/55587\n * then it will be returned 55587\n *\n * @param gid - gid://qeepsake-rails/Model/55587\n * @param stripTimestamp - whether to strip the timestamp from the ID (i.e. %2C2022-09-26+13%3A08%3A27+UTC)\n *\n * @returns 55587\n */\nexport function extractRailsId(gid: string, stripTimestamp?: boolean): string\nexport function extractRailsId(\n gid?: string | null,\n stripTimestamp?: boolean,\n): string | null\nexport function extractRailsId(\n gid?: string | null,\n stripTimestamp = false,\n): string | null {\n // Checks if ID has already been extracted, if so, return it\n if(gid && isExtractedRailsId(gid)) {\n return gid\n }\n\n if (typeof gid === 'string' && gid?.indexOf('/') != -1) {\n const gidComponents = gid.split('/')\n let extractedId = gidComponents[gidComponents.length - 1]\n\n // Strips the timestamp (%2C2022-09-26+13%3A08%3A27+UTC)) from the ID\n if (stripTimestamp && extractedId?.includes('%')) {\n extractedId = extractedId.split('%')[0]\n }\n\n if (extractedId) {\n return extractedId\n }\n }\n\n return null\n}\n","/**\n * Creates a rails id\n *\n * If the ID is passed in the format of 55587, then it will be returned\n * gid://qeepsake-rails/JournalEntry/55587\n *\n * @param id - 55587\n * @param model - JournalEntry\n *\n * @returns gid://qeepsake-rails/Model/55587\n */\nexport function createRailsId(id: number | string, model: string): string {\n return `gid://qeepsake-rails/${model}/${id}`\n}\n"],"names":["isExtractedRailsId","id","startsWith","extractRailsId","gid","stripTimestamp","indexOf","gidComponents","split","extractedId","length","includes","createRailsId","model"],"mappings":"SAQgBA,mBAAmBC;AACjC,SAAO,EAACA,EAAD,aAACA,EAAD,eAACA,EAAE,CAAEC,UAAJ,CAAe,QAAf,CAAD,CAAP;AACD;;SCQeC,eACdC,KACAC;MAAAA;AAAAA,IAAAA,iBAAiB;;;AAGjB,MAAGD,GAAG,IAAIJ,kBAAkB,CAACI,GAAD,CAA5B,EAAmC;AACjC,WAAOA,GAAP;AACD;;AAED,MAAI,OAAOA,GAAP,KAAe,QAAf,IAA2B,CAAAA,GAAG,SAAH,IAAAA,GAAG,WAAH,YAAAA,GAAG,CAAEE,OAAL,CAAa,GAAb,MAAqB,CAAC,CAArD,EAAwD;AAAA;;AACtD,QAAMC,aAAa,GAAGH,GAAG,CAACI,KAAJ,CAAU,GAAV,CAAtB;AACA,QAAIC,WAAW,GAAGF,aAAa,CAACA,aAAa,CAACG,MAAd,GAAuB,CAAxB,CAA/B;;AAGA,QAAIL,cAAc,oBAAII,WAAJ,yCAAI,aAAaE,QAAb,CAAsB,GAAtB,CAAtB,EAAkD;AAChDF,MAAAA,WAAW,GAAGA,WAAW,CAACD,KAAZ,CAAkB,GAAlB,EAAuB,CAAvB,CAAd;AACD;;AAED,QAAIC,WAAJ,EAAiB;AACf,aAAOA,WAAP;AACD;AACF;;AAED,SAAO,IAAP;AACD;;SC/BeG,cAAcX,IAAqBY;AACjD,mCAA+BA,KAA/B,SAAwCZ,EAAxC;AACD;;;;"} \ No newline at end of file diff --git a/lib/is-extracted-rails-id.d.ts b/lib/is-extracted-rails-id.d.ts new file mode 100644 index 0000000..98c9b8e --- /dev/null +++ b/lib/is-extracted-rails-id.d.ts @@ -0,0 +1,8 @@ +/** + * Checks to see if the given string is a valid extracted Rails ID + * i.e. 55587 and not gid://qeepsake-rails/JournalEntry/55587 + * + * @param id - rails ID + * @returns if ID is extracted rails ID + */ +export declare function isExtractedRailsId(id: string): boolean; diff --git a/lib/is-extracted-rails-id.js b/lib/is-extracted-rails-id.js new file mode 100644 index 0000000..466ba9e --- /dev/null +++ b/lib/is-extracted-rails-id.js @@ -0,0 +1,14 @@ +/** + * Checks to see if the given string is a valid extracted Rails ID + * i.e. 55587 and not gid://qeepsake-rails/JournalEntry/55587 + * + * @param id - rails ID + * @returns if ID is extracted rails ID + */ +export function isExtractedRailsId(id) { + if (typeof id === 'string') { + // Extracted IDs should NOT start with gid:// with no empty spaces + return !id.startsWith('gid://') && id.indexOf(' ') == -1; + } + return false; +} diff --git a/src/extract-rails-id.ts b/src/extract-rails-id.ts index 6fe5cd0..44b1b36 100644 --- a/src/extract-rails-id.ts +++ b/src/extract-rails-id.ts @@ -1,3 +1,5 @@ +import { isExtractedRailsId } from './is-extracted-rails-id' + /** * Extract rails ID * @@ -18,6 +20,9 @@ export function extractRailsId( gid?: string | null, stripTimestamp = false, ): string | null { + // Checks if ID has already been extracted, if so, return it + if (gid && isExtractedRailsId(gid)) return gid + if (typeof gid === 'string' && gid?.indexOf('/') != -1) { const gidComponents = gid.split('/') let extractedId = gidComponents[gidComponents.length - 1] diff --git a/src/index.ts b/src/index.ts index 891a675..14f7359 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export { extractRailsId } from './extract-rails-id' export { createRailsId } from './create-rails-id' +export { isExtractedRailsId } from './is-extracted-rails-id' diff --git a/src/is-extracted-rails-id.ts b/src/is-extracted-rails-id.ts new file mode 100644 index 0000000..ff02d86 --- /dev/null +++ b/src/is-extracted-rails-id.ts @@ -0,0 +1,15 @@ +/** + * Checks to see if the given string is a valid extracted Rails ID + * i.e. 55587 and not gid://qeepsake-rails/JournalEntry/55587 + * + * @param id - rails ID + * @returns if ID is extracted rails ID + */ +export function isExtractedRailsId(id: string): boolean { + if (typeof id === 'string') { + // Extracted IDs should NOT start with gid:// with no empty spaces + return !id.startsWith('gid://') && id.indexOf(' ') == -1 + } + + return false +} diff --git a/tests/is-extracted-rails-id.test.js b/tests/is-extracted-rails-id.test.js new file mode 100644 index 0000000..0b12d00 --- /dev/null +++ b/tests/is-extracted-rails-id.test.js @@ -0,0 +1,9 @@ +const { isExtractedRailsId } = require('../src/is-extracted-rails-id'); + +test('Test non-extracted Rails ID equals false', () => { + expect(isExtractedRailsId('gid://qeepsake-rails/MyModel/12345')).toBe(false); +}); + +test('Test extracted Rails ID equals true', () => { + expect(isExtractedRailsId('12345')).toBe(true); +}); \ No newline at end of file