From 4b25615acc308dc25deefe52c12ad0a657c7f99e Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Wed, 7 Jul 2021 10:42:19 +0700 Subject: [PATCH] [fix] correct `ReadOnlyFormData` generator implementation (#1837) --- .changeset/blue-llamas-exist.md | 5 ++ .../server/parse_body/read_only_form_data.js | 8 +-- .../parse_body/read_only_form_data.spec.js | 59 +++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 .changeset/blue-llamas-exist.md create mode 100644 packages/kit/src/runtime/server/parse_body/read_only_form_data.spec.js diff --git a/.changeset/blue-llamas-exist.md b/.changeset/blue-llamas-exist.md new file mode 100644 index 000000000000..f83044e56b21 --- /dev/null +++ b/.changeset/blue-llamas-exist.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Fix ReadOnlyFormData keys and values method implementation diff --git a/packages/kit/src/runtime/server/parse_body/read_only_form_data.js b/packages/kit/src/runtime/server/parse_body/read_only_form_data.js index eced5918d1bf..3268c4ffcc5c 100644 --- a/packages/kit/src/runtime/server/parse_body/read_only_form_data.js +++ b/packages/kit/src/runtime/server/parse_body/read_only_form_data.js @@ -61,17 +61,13 @@ class ReadOnlyFormData { } *keys() { - for (const [key, value] of this.#map) { - for (let i = 0; i < value.length; i += 1) { - yield key; - } - } + for (const [key] of this.#map) yield key; } *values() { for (const [, value] of this.#map) { for (let i = 0; i < value.length; i += 1) { - yield value; + yield value[i]; } } } diff --git a/packages/kit/src/runtime/server/parse_body/read_only_form_data.spec.js b/packages/kit/src/runtime/server/parse_body/read_only_form_data.spec.js new file mode 100644 index 000000000000..a792e00967b4 --- /dev/null +++ b/packages/kit/src/runtime/server/parse_body/read_only_form_data.spec.js @@ -0,0 +1,59 @@ +import { test } from 'uvu'; +import * as assert from 'uvu/assert'; +import { read_only_form_data } from './read_only_form_data.js'; + +const { data, append } = read_only_form_data(); +append('foo', '1'), append('foo', '2'), append('foo', '3'); +append('bar', '2'), append('bar', '1'); + +test('ro-fd get returns first value', () => { + assert.equal(data.get('foo'), '1'); + assert.equal(data.get('bar'), '2'); +}); + +test('ro-fd getAll returns array', () => { + assert.equal(data.getAll('foo'), ['1', '2', '3']); + assert.equal(data.getAll('bar'), ['2', '1']); +}); + +test('ro-fd has returns boolean flag', () => { + assert.equal(data.has('foo'), true); + assert.equal(data.has('bar'), true); + assert.equal(data.has('baz'), false); +}); + +test('ro-fd iterator yields all key-value pairs', () => { + const values = []; + for (const [key, val] of data) values.push({ key, val }); + + assert.equal(values.length, 5); + assert.equal(values[0], { key: 'foo', val: '1' }); + assert.equal(values[3], { key: 'bar', val: '2' }); +}); + +test('ro-fd entries() yields all key-value pairs', () => { + const values = []; + for (const [key, val] of data.entries()) values.push({ key, val }); + + assert.equal(values.length, 5); + assert.equal(values[0], { key: 'foo', val: '1' }); + assert.equal(values[3], { key: 'bar', val: '2' }); +}); + +test('ro-fd keys() yields all unique keys', () => { + const values = []; + for (const key of data.keys()) values.push(key); + + assert.equal(values.length, 2); + assert.equal(values, ['foo', 'bar']); +}); + +test('ro-fd values() yields all nested values', () => { + const values = []; + for (const val of data.values()) values.push(val); + + assert.equal(values.length, 5); + assert.equal(values, ['1', '2', '3', '2', '1']); +}); + +test.run();