Skip to content

Commit 032cb25

Browse files
authored
Merge pull request #35 from silwalanish/implement-short-id
Add implementation to get short id
2 parents 38ea1a5 + dc0ca9c commit 032cb25

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ Example: If used in express, it returns unique store id per request.
227227
const requestIdentifier = store.getId();
228228
```
229229

230+
### getShortId()
231+
232+
Gets the short unique store id created for the current context/scope.
233+
234+
Note: This is same as `getId();` the difference being it only returns the first 8 characters.
235+
236+
- `@returns {string | undefined}` - Returns the short unique store id.
237+
238+
```js
239+
const requestIdentifier = store.getShortId();
240+
```
241+
230242
## Changelog
231243

232244
Check the [CHANGELOG](CHANGELOG.md) for release history.

src/AsyncStore.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface AsyncStore {
1212
find: (key: string) => any;
1313
isInitialized: () => boolean;
1414
getId: () => string | undefined;
15+
getShortId: () => string | undefined;
1516
}
1617

1718
export default AsyncStore;

src/impl/domain.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,19 @@ export function getId(): string | undefined {
234234
return activeDomain && activeDomain[ID_KEY];
235235
}
236236

237+
/**
238+
* Gets the short unique domain id created for the current context / scope.
239+
*
240+
* Note: This is same as `getId();` the difference being it only returns the first 8 characters.
241+
*
242+
* @returns {(string | undefined)}
243+
*/
244+
export function getShortId(): string | undefined {
245+
const id = getId();
246+
247+
return id && id.substring(0, 8);
248+
}
249+
237250
/**
238251
* Get the store from the active domain.
239252
*

src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ export function getId(): string | undefined {
199199
return initializedAdapter && getInstance(initializedAdapter).getId();
200200
}
201201

202+
/**
203+
* Gets the short unique domain id created for the current context / scope.
204+
*
205+
* Note: This is same as `getId();` the difference being it only returns the first 8 characters.
206+
*
207+
* @returns {(string | undefined)}
208+
*/
209+
export function getShortId(): string | undefined {
210+
return initializedAdapter && getInstance(initializedAdapter).getShortId();
211+
}
212+
202213
/**
203214
* Get the adapter instance based on adapter type.
204215
*

test/domain.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,28 @@ describe('store: [adapter=DOMAIN]', () => {
212212
});
213213
});
214214

215+
describe('getShortId()', () => {
216+
it('should return short (8 chars) unique value if store is initialized.', done => {
217+
const callback = () => {
218+
expect(globalStore.getShortId()).to.be.an('string');
219+
expect(globalStore.getShortId()).to.not.equal(null);
220+
expect(globalStore.getShortId()).to.not.equal(undefined);
221+
expect(globalStore.getShortId()).to.be.lengthOf(8);
222+
223+
done();
224+
};
225+
226+
globalStore.initialize(adapter)(callback);
227+
});
228+
229+
it('should return `undefined` if store is not initialized.', done => {
230+
expect(globalStore.getShortId).to.not.throw();
231+
expect(globalStore.getShortId()).to.equal(undefined);
232+
233+
done();
234+
});
235+
});
236+
215237
describe('find()', () => {
216238
it('should successfully return value in synchronous callback.', done => {
217239
const callback = () => {

0 commit comments

Comments
 (0)