From 8c921f79da879fdf251116cc8335a1f2b670bd09 Mon Sep 17 00:00:00 2001 From: phil Date: Tue, 31 May 2016 10:11:56 -0400 Subject: [PATCH 1/2] Expose options on the Union instance This change eats up another key that now can't be an option member ('options'), in order to allow code to do some neat inspection of the union. The motivating example: creating a copy of the union whose constructors are bound to do extra stuff. All I wanted was to iterate over the keys of the original. Now it's possible :) It's recommended to use capital-case naming for union option members, so as long as that convention is followed, there is no new naming collision here. --- index.es6 | 4 ++++ index.js | 4 ++++ test.js | 13 +++++++++++++ 3 files changed, 21 insertions(+) diff --git a/index.es6 b/index.es6 index 6adf7a6..886645c 100644 --- a/index.es6 +++ b/index.es6 @@ -130,6 +130,9 @@ function Union(options, proto={}, static_={}, factory=_factory) { if (options.hasOwnProperty('match')) { throw new Error('Cannot use reserved name `match` as part of a Union'); } + if (options.hasOwnProperty('options')) { + throw new UnionError('Cannot use reserved name `options` as part of a Union'); + } if (options.hasOwnProperty('OptionClass')) { throw new Error('Cannot use reserved name `UnionClass` as part of a Union'); } @@ -154,6 +157,7 @@ function Union(options, proto={}, static_={}, factory=_factory) { UnionOption.unionFactory = Union; const union = { + options: options, OptionClass: UnionOption, toString: () => `[Union { ${Object.keys(options).join(', ')} }]`, match, diff --git a/index.js b/index.js index eb21c0c..549a123 100644 --- a/index.js +++ b/index.js @@ -160,6 +160,9 @@ function Union(options) { if (options.hasOwnProperty('match')) { throw new Error('Cannot use reserved name `match` as part of a Union'); } + if (options.hasOwnProperty('options')) { + throw new UnionError('Cannot use reserved name `options` as part of a Union'); + } if (options.hasOwnProperty('OptionClass')) { throw new Error('Cannot use reserved name `UnionClass` as part of a Union'); } @@ -207,6 +210,7 @@ function Union(options) { UnionOption.unionFactory = Union; var union = _extends({ + options: options, OptionClass: UnionOption, toString: function toString() { return '[Union { ' + Object.keys(options).join(', ') + ' }]'; diff --git a/test.js b/test.js index 5300610..0c7bd30 100644 --- a/test.js +++ b/test.js @@ -124,6 +124,19 @@ describe('Union', () => { } }); }); + describe('.options', () => { + it('should be exposed', () => { + const U = Union({A: null}); + assert.deepEqual(U.options, {A: null}); + }); + it('should have all the options', () => { + const U = Union({A: null, B: null, C: null}); + assert.deepEqual(U.options, {A: null, B: null, C: null}); + }); + it('should throw if constructed with `options` as an option', () => { + assert.throws(() => Union({options: null})); + }); + }); describe('errors thrown by results', () => { it('should be instance of Error', () => { assert.throws(() => Union()); // no members From f3ff5f01003db69a5a23c3a9e594d9d924a9ecae Mon Sep 17 00:00:00 2001 From: phil Date: Tue, 31 May 2016 10:22:21 -0400 Subject: [PATCH 2/2] Update the docs and version number --- changelog.md | 9 +++++++++ readme.md | 11 +++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 7a895c1..1fb8b7d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,15 @@ Changes ======= +### v0.14.0 + +2016-05-31 + +### Breaking changes + + * `options` is now a reserved key that can't be used as a union member. + + ### v0.13.0 2016-03-23 diff --git a/readme.md b/readme.md index 1143cec..8ea5b1d 100644 --- a/readme.md +++ b/readme.md @@ -290,11 +290,18 @@ Stoplight.match(Stoplight.Green(), { ``` +#### `options` static property on `union` object + +After creating a `union` object, the `.options` property references an object +containing keys for each union option specified. It's not usually that useful +unless you want to introspect the union and see what options it has -- powerful, +but usually not necessary! + + ### `OptionClass()` constructor A function for creating OptionClass instances. You should not call this -constructor directly, but rather use one of the factories attached to the -`union` object via keys named after the union's members. +constructor directly -- it's exposed just for `instanceof` checks. In the `Stoplight` example above, the following is ok: