From 081bdd98f76c81afa14d47a28f013c013e7248ed Mon Sep 17 00:00:00 2001 From: Josiah Savary Date: Wed, 8 Nov 2017 16:02:56 -0500 Subject: [PATCH 1/2] Make Results iterable It'd be very convenient to be able to pull values out of Results via array destructuring. --- src/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index e112b57..cb20f27 100644 --- a/src/index.js +++ b/src/index.js @@ -18,7 +18,15 @@ const coderArray = utils.coderArray; const paramTypePart = utils.paramTypePart; const getParamCoder = utils.getParamCoder; -function Result() {} +function Result() { + this[Symbol.iterator] = function* () { + const keys = Object.keys(this); + for (key of keys) { + if (!this.hasOwnProperty(key) || !Number.isFinite(Number(key))) continue + yield this[key]; + } + } +} function encodeParams(types, values) { if (types.length !== values.length) { From 1056ed5b9bba789033cf66aad25f38fd142c65fd Mon Sep 17 00:00:00 2001 From: Josiah Savary Date: Mon, 20 Nov 2017 10:41:13 -0500 Subject: [PATCH 2/2] Removed generator-based iterator Added an plain object iterator to avoid minifier issues --- src/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index cb20f27..9749669 100644 --- a/src/index.js +++ b/src/index.js @@ -19,11 +19,14 @@ const paramTypePart = utils.paramTypePart; const getParamCoder = utils.getParamCoder; function Result() { - this[Symbol.iterator] = function* () { - const keys = Object.keys(this); - for (key of keys) { - if (!this.hasOwnProperty(key) || !Number.isFinite(Number(key))) continue - yield this[key]; + this[Symbol.iterator] = () => { + const keys = Object.keys(this) + .filter(key => Number.isFinite(Number(key))) + return { + next: () => { + if (!keys.length) return { value: undefined, done: true } + return { value: this[keys.shift()], done: false } + } } } }