Skip to content

Commit

Permalink
feat(apply-context): support objects with toJSON method as context va…
Browse files Browse the repository at this point in the history
…riables (#88)
  • Loading branch information
Hybrid-Force committed Aug 18, 2022
1 parent 44a3ca4 commit ecb3dbc
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 11 deletions.
5 changes: 4 additions & 1 deletion dist/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
* @example
* ```javascript
* const context = {
* user: { id: 456, bestFriends: [123, 532, 987] }
* user: { id: 456, bestFriends: [123, 532, 987], oid: new ObjectId('62fe296afd3ad81be5088699') },
* };
* applyContext('secrets:${user.id}:*', context)
* // => 'secrets:456:*'
*
* applyContext('secrets:${user.bestFriends}:*', context)
* // => 'secrets:{123,532,987}:*'
*
* applyContext('secrets:${user.oid}:*', context)
* // => 'secrets:62fe296afd3ad81be5088699:*'
*
* applyContext('secrets:${company.address}:account', context)
* // => 'secrets:undefined:account'
* ```
Expand Down
11 changes: 8 additions & 3 deletions dist/main.es.js

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

2 changes: 1 addition & 1 deletion dist/main.es.js.map

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions dist/main.js

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

2 changes: 1 addition & 1 deletion dist/main.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@typescript-eslint/parser": "4.5.0",
"babel-eslint": "10.0.3",
"babel-jest": "26.2.2",
"bson": "^4.6.5",
"commitizen": "4.2.4",
"coveralls": "3.1.1",
"cz-conventional-changelog": "3.3.0",
Expand Down
11 changes: 11 additions & 0 deletions src/utils/applyContext.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ObjectId } from 'bson';
import { applyContext } from './applyContext';

describe('applyContext', () => {
Expand Down Expand Up @@ -43,4 +44,14 @@ describe('applyContext', () => {
'secrets:undefined:account'
);
});

it('can match ObjectId', () => {
const context = {
user: { id: 456, oid: new ObjectId('62fe296afd3ad81be5088699') }
};

expect(applyContext('secrets:${user.oid}:account', context)).toBe(
'secrets:62fe296afd3ad81be5088699:account'
);
});
});
10 changes: 8 additions & 2 deletions src/utils/applyContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ const specialTrim = (str: string): string => str.replace(trim, '');
* @example
* ```javascript
* const context = {
* user: { id: 456, bestFriends: [123, 532, 987] }
* user: { id: 456, bestFriends: [123, 532, 987], oid: new ObjectId('62fe296afd3ad81be5088699') },
* };
* applyContext('secrets:${user.id}:*', context)
* // => 'secrets:456:*'
*
* applyContext('secrets:${user.bestFriends}:*', context)
* // => 'secrets:{123,532,987}:*'
*
* applyContext('secrets:${user.oid}:*', context)
* // => 'secrets:62fe296afd3ad81be5088699:*'
*
* applyContext('secrets:${company.address}:account', context)
* // => 'secrets:undefined:account'
* ```
Expand All @@ -36,7 +39,10 @@ export function applyContext<T extends object>(
str.replace(reDelimiters, (_, path: string) => {
const value = getValueFromPath(context, path);
if (Array.isArray(value)) return `{${value}}`;
if (value instanceof Object) return 'undefined';
if (value instanceof Object) {
const json = value.toJSON ? value.toJSON() : undefined;
return typeof json === 'string' ? json : 'undefined';
}

return String(value);
})
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2480,6 +2480,11 @@ balanced-match@^1.0.0:
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==

base64-js@^1.3.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==

base@^0.11.1:
version "0.11.2"
resolved "https://registry.yarnpkg.com/base/-/base-0.11.2.tgz#7bde5ced145b6d551a90db87f83c558b4eb48a8f"
Expand Down Expand Up @@ -2581,11 +2586,26 @@ bser@2.1.1:
dependencies:
node-int64 "^0.4.0"

bson@^4.6.5:
version "4.6.5"
resolved "https://registry.yarnpkg.com/bson/-/bson-4.6.5.tgz#1a410148c20eef4e40d484878a037a7036e840fb"
integrity sha512-uqrgcjyOaZsHfz7ea8zLRCLe1u+QGUSzMZmvXqO24CDW7DWoW1qiN9folSwa7hSneTSgM2ykDIzF5kcQQ8cwNw==
dependencies:
buffer "^5.6.0"

buffer-from@^1.0.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5"
integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==

buffer@^5.6.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0"
integrity sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==
dependencies:
base64-js "^1.3.1"
ieee754 "^1.1.13"

builtin-modules@^3.1.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-3.2.0.tgz#45d5db99e7ee5e6bc4f362e008bf917ab5049887"
Expand Down Expand Up @@ -4666,6 +4686,11 @@ iconv-lite@^0.6.2:
dependencies:
safer-buffer ">= 2.1.2 < 3.0.0"

ieee754@^1.1.13:
version "1.2.1"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==

ignore-walk@^3.0.3:
version "3.0.4"
resolved "https://registry.yarnpkg.com/ignore-walk/-/ignore-walk-3.0.4.tgz#c9a09f69b7c7b479a5d74ac1a3c0d4236d2a6335"
Expand Down

0 comments on commit ecb3dbc

Please sign in to comment.