Skip to content

Commit

Permalink
feat: expose trapAccess (#40)
Browse files Browse the repository at this point in the history
* feat: expose trapAccess

* Update src/__tests__/trapAccess.spec.ts
  • Loading branch information
P0lip authored Mar 17, 2020
1 parent d59b0a4 commit 8d9b878
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/__tests__/trapAccess.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { KEYS, trapAccess } from '../trapAccess';

describe('trapAccess', () => {
test('given no KEYS, should use own keys in ownKeys trap', () => {
const obj = trapAccess({ '404': null, '200': null });
expect(Reflect.ownKeys(obj)).toEqual(['200', '404']);
});

test('given KEYS, should use KEYS in ownKeys trap', () => {
const obj = trapAccess({ '404': null, '200': null, [KEYS]: ['404', '200'] });
expect(Reflect.ownKeys(obj)).toEqual(['404', '200']);
});
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './parse';
export * from './parseWithPointers';
export * from './safeStringify';
export * from './types';
export * from './trapAccess';
11 changes: 2 additions & 9 deletions src/parseWithPointers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { buildJsonPath } from './buildJsonPath';
import { SpecialMappingKeys } from './consts';
import { dereferenceAnchor } from './dereferenceAnchor';
import { lineForPosition } from './lineForPosition';
import { KEYS, trapAccess } from './trapAccess';
import { IParseOptions, Kind, ScalarType, YAMLMapping, YAMLNode, YamlParserResult, YAMLScalar } from './types';
import { isObject } from './utils';

Expand Down Expand Up @@ -48,8 +49,6 @@ export const parseWithPointers = <T>(value: string, options?: IParseOptions): Ya
return parsed;
};

const KEYS = Symbol('object_keys');

export const walkAST = (
node: YAMLNode | null,
options: Optional<IParseOptions>,
Expand Down Expand Up @@ -221,15 +220,9 @@ const reduceMergeKeys = (items: unknown, preserveKeyOrder: boolean): object | nu
return typeof items !== 'object' || items === null ? null : Object(items);
};

const traps = {
ownKeys(target: object) {
return target[KEYS];
},
};

function createMapContainer(preserveKeyOrder: boolean): { [key in PropertyKey]: unknown } {
if (preserveKeyOrder) {
const container = new Proxy({}, traps);
const container = trapAccess({});
Reflect.defineProperty(container, KEYS, {
value: [],
});
Expand Down
9 changes: 9 additions & 0 deletions src/trapAccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const KEYS = Symbol('object_keys');

const traps = {
ownKeys(target: object) {
return KEYS in target ? target[KEYS] : Reflect.ownKeys(target);
},
};

export const trapAccess = <T extends object = object>(target: T): T => new Proxy<T>(target, traps);

0 comments on commit 8d9b878

Please sign in to comment.