Skip to content

Commit

Permalink
feat(api): return ID from extractRailsId if it is already extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Farrell committed Sep 26, 2022
1 parent f7bc62e commit 43f9634
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/extract-rails-id.js
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { extractRailsId } from './extract-rails-id';
export { createRailsId } from './create-rails-id';
export { isExtractedRailsId } from './is-extracted-rails-id';
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { extractRailsId } from './extract-rails-id';
export { createRailsId } from './create-rails-id';
export { isExtractedRailsId } from './is-extracted-rails-id';
2 changes: 1 addition & 1 deletion lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion lib/index.modern.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.modern.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/is-extracted-rails-id.d.ts
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions lib/is-extracted-rails-id.js
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 5 additions & 0 deletions src/extract-rails-id.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isExtractedRailsId } from './is-extracted-rails-id'

/**
* Extract rails ID
*
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { extractRailsId } from './extract-rails-id'
export { createRailsId } from './create-rails-id'
export { isExtractedRailsId } from './is-extracted-rails-id'
15 changes: 15 additions & 0 deletions src/is-extracted-rails-id.ts
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions tests/is-extracted-rails-id.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 43f9634

Please sign in to comment.