From 089f32f8ea93194c2c839df13c8e2d398cfdb4ac Mon Sep 17 00:00:00 2001 From: jalik Date: Sat, 2 Dec 2017 14:41:46 -1000 Subject: [PATCH] Renames option `parseValues` to `dynamicTyping` in `parseForm()` method Renames option `smartParsing` to `smartTyping` in `parseForm()` method Renames option `trimValues` to `trim` in `parseForm()` method --- README.md | 13 +++-- package.json | 2 +- src/jk-form-parser.js | 26 +++++++--- test/jk-form-parser.test.js | 97 ++++++++++++++++++++++--------------- 4 files changed, 87 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index c223446..a481526 100755 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ First of all, you have to define fields types in the HTML code using the `data-t ``` -If no `data-type` attribute is set, the `type` attribute will be used (for `input` elements at least), this behavior is active by default with the combination of this options `{parseValues: true, smartParsing: true}` in the `parseForm()` function. +If no `data-type` attribute is set, the `type` attribute will be used (for `input` elements at least), this behavior is active by default with the combination of this options `{dynamicTyping: true, smartTyping: true}` in the `parseForm()` function. ## Getting fields from a form @@ -73,11 +73,11 @@ const fields = FormUtils.parseForm(form, { // Replace empty strings with null nullify: true, // Parse values to the best type (ex: "001" => 1) - parseValues: true, + dynamicTyping: true, // Parse values based on field type (ex: type="number" will parse to number) - smartParsing: true, + smartTyping: true, // Remove extra spaces - trimValues: true + trim: true }); ``` @@ -367,6 +367,11 @@ const fields = FormUtils.parseForm(form, { ## Changelog +### v1.0.4 +- Renames option `parseValues` to `dynamicTyping` in `parseForm()` method +- Renames option `smartParsing` to `smartTyping` in `parseForm()` method +- Renames option `trimValues` to `trim` in `parseForm()` method + ### v1.0.3 - Fixes error with unchecked fields being parsed - Changes signature of method `cleanFunction(value, field)` called by `parseForm()` diff --git a/package.json b/package.json index 58660b5..ed1a5a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jk-form-parser", - "version": "1.0.3", + "version": "1.0.4", "description": "Parse complex forms with minimum effort.", "license": "MIT", "keywords": [ diff --git a/src/jk-form-parser.js b/src/jk-form-parser.js index ee211fe..ea17eda 100755 --- a/src/jk-form-parser.js +++ b/src/jk-form-parser.js @@ -107,17 +107,31 @@ export default { // Default options options = this.extend({ cleanFunction: null, + dynamicTyping: true, filterFunction: null, ignoreButtons: true, ignoreDisabled: true, ignoreEmpty: false, ignoreUnchecked: false, nullify: true, - parseValues: true, - smartParsing: true, - trimValues: true + smartTyping: true, + trim: true }, options); + // Check deprecated options + if (options.hasOwnProperty("parseValues")) { + console.warn(`option "parseValues" is deprecated, rename it to "dynamicTyping" instead`); + options.dynamicTyping = options.parseValues; + } + if (options.hasOwnProperty("smartParsing")) { + console.warn(`option "smartParsing" is deprecated, rename it to "smartTyping" instead`); + options.smartTyping = options.smartParsing; + } + if (options.hasOwnProperty("trimValues")) { + console.warn(`option "trimValues" is deprecated, rename it to "trim" instead`); + options.trim = options.trimValues; + } + let fields = {}; const elements = form.elements; @@ -178,7 +192,7 @@ export default { break; } - if (options.parseValues) { + if (options.dynamicTyping) { // Parse value excepted for special fields if ((!isCheckable || field.checked) && !this.contains(["email", "file", "password", "search", "url"], field.type) @@ -226,7 +240,7 @@ export default { } } // Parse value using the "type" attribute - else if (options.smartParsing) { + else if (options.smartTyping) { switch (field.type) { case "number": case "range": @@ -253,7 +267,7 @@ export default { } // Removes extra spaces - if (options.trimValues && !this.contains(["password"], field.type)) { + if (options.trim && !this.contains(["password"], field.type)) { if (value instanceof Array) { for (let k = 0; k < value.length; k += 1) { if (typeof value[k] === "string" && value[k].length) { diff --git a/test/jk-form-parser.test.js b/test/jk-form-parser.test.js index 0bc97e8..bd8aebc 100755 --- a/test/jk-form-parser.test.js +++ b/test/jk-form-parser.test.js @@ -231,7 +231,24 @@ describe("parseBoolean()", () => { describe("parseForm()", () => { - describe("Parsing fields with options {parseValues: true, smartParsing: true}", () => { + test(`should return empty array if no checkbox is checked`, () => { + const form = TestUtils.createForm(); + form.appendChild(TestUtils.createCheckbox({ + checked: false, + name: "items[]", + value: 0 + })); + form.appendChild(TestUtils.createCheckbox({ + checked: false, + name: "items[]", + value: 1 + })); + + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); + expect(r).toEqual({items: []}); + }); + + describe("Parsing fields with options {dynamicTyping: true, smartTyping: true}", () => { test(`parseForm(form, options) should not return values of unknown fields`, () => { const form = TestUtils.createForm(); @@ -239,7 +256,7 @@ describe("parseForm()", () => { value: STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({}); }); @@ -251,7 +268,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({number: INTEGER_STRING}); }); @@ -273,7 +290,7 @@ describe("parseForm()", () => { value: "C" })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({ checkboxes: [ form.elements["0"].value, @@ -295,7 +312,7 @@ describe("parseForm()", () => { value: "B" })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({radio: form.elements["1"].value}); }); @@ -332,7 +349,7 @@ describe("parseForm()", () => { value: "2" })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); console.log(r); expect(r).toEqual({ options: {} @@ -346,7 +363,7 @@ describe("parseForm()", () => { value: STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({file: STRING}); }); @@ -357,7 +374,7 @@ describe("parseForm()", () => { value: STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({hidden: STRING}); }); @@ -368,7 +385,7 @@ describe("parseForm()", () => { value: INTEGER })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({number: INTEGER}); }); @@ -380,7 +397,7 @@ describe("parseForm()", () => { value: STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({email: STRING}); }); @@ -391,7 +408,7 @@ describe("parseForm()", () => { value: STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({password: STRING}); }); @@ -405,7 +422,7 @@ describe("parseForm()", () => { ] })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({select: "B"}); }); @@ -421,7 +438,7 @@ describe("parseForm()", () => { ] })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({select: ["B", "C"]}); }); @@ -432,7 +449,7 @@ describe("parseForm()", () => { value: STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({text: STRING}); }); @@ -443,7 +460,7 @@ describe("parseForm()", () => { value: STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({textarea: STRING}); }); }); @@ -633,7 +650,7 @@ describe("parseForm()", () => { value: " " })); - const r = FormUtils.parseForm(form, {nullify: true, trimValues: true}); + const r = FormUtils.parseForm(form, {nullify: true, trim: true}); expect(r).toEqual({text: null}); }); @@ -644,14 +661,14 @@ describe("parseForm()", () => { value: " " })); - const r = FormUtils.parseForm(form, {nullify: false, trimValues: true}); + const r = FormUtils.parseForm(form, {nullify: false, trim: true}); expect(r).toEqual({text: ""}); }); }); - describe("parseValues option", () => { + describe("dynamicTyping option", () => { - test(`parseForm(form, {parseValues: true, smartParsing: false}) should parse all values`, () => { + test(`parseForm(form, {dynamicTyping: true, smartTyping: false}) should parse all values`, () => { const form = TestUtils.createForm(); form.appendChild(TestUtils.createTextInput({ dataset: {type: "boolean"}, @@ -672,7 +689,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: false}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: false}); expect(r).toEqual({ bool_true: true, bool_false: false, @@ -681,7 +698,7 @@ describe("parseForm()", () => { }); }); - test(`parseForm(form, {parseValues: false}) should not parse any value`, () => { + test(`parseForm(form, {dynamicTyping: false}) should not parse any value`, () => { const form = TestUtils.createForm(); form.appendChild(TestUtils.createTextInput({ dataset: {type: "boolean"}, @@ -702,7 +719,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: false}); + const r = FormUtils.parseForm(form, {dynamicTyping: false}); expect(r).toEqual({ bool_true: TRUE, bool_false: FALSE, @@ -712,9 +729,9 @@ describe("parseForm()", () => { }); }); - describe("smartParsing option", () => { + describe("smartTyping option", () => { - test(`parseForm(form, {smartParsing: true}) should parse values using type attribute`, () => { + test(`parseForm(form, {smartTyping: true}) should parse values using type attribute`, () => { const form = TestUtils.createForm(); form.appendChild(TestUtils.createTextInput({ name: "bool_true", @@ -742,7 +759,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: true}); expect(r).toEqual({ bool_true: TRUE, bool_false: false, @@ -753,7 +770,7 @@ describe("parseForm()", () => { }); }); - test(`parseForm(form, {smartParsing: false}) should not parse values using type attribute`, () => { + test(`parseForm(form, {smartTyping: false}) should not parse values using type attribute`, () => { const form = TestUtils.createForm(); form.appendChild(TestUtils.createTextInput({ name: "bool_true", @@ -781,7 +798,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true, smartParsing: false}); + const r = FormUtils.parseForm(form, {dynamicTyping: true, smartTyping: false}); expect(r).toEqual({ bool_true: true, bool_false: false, @@ -793,38 +810,38 @@ describe("parseForm()", () => { }); }); - describe("trimValues option", () => { + describe("trim option", () => { - test(`parseForm(form, {trimValues: true}) should trim text values`, () => { + test(`parseForm(form, {trim: true}) should trim text values`, () => { const form = TestUtils.createForm(); form.appendChild(TestUtils.createTextInput({ name: "text", value: ` ${STRING} ` })); - const r = FormUtils.parseForm(form, {trimValues: true}); + const r = FormUtils.parseForm(form, {trim: true}); expect(r).toEqual({text: STRING}); }); - test(`parseForm(form, {trimValues: false}) should not trim text values`, () => { + test(`parseForm(form, {trim: false}) should not trim text values`, () => { const form = TestUtils.createForm(); form.appendChild(TestUtils.createTextInput({ name: "text", value: ` ${STRING} ` })); - const r = FormUtils.parseForm(form, {trimValues: false}); + const r = FormUtils.parseForm(form, {trim: false}); expect(r).toEqual({text: ` ${STRING} `}); }); - test(`parseForm(form, {trimValues: true}) should not trim password values`, () => { + test(`parseForm(form, {trim: true}) should not trim password values`, () => { const form = TestUtils.createForm(); form.appendChild(TestUtils.createPasswordInput({ name: "password", value: ` ${STRING} ` })); - const r = FormUtils.parseForm(form, {trimValues: true}); + const r = FormUtils.parseForm(form, {trim: true}); expect(r).toEqual({password: ` ${STRING} `}); }); }); @@ -840,7 +857,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true}); expect(r).toEqual({email: INTEGER_STRING}); }); @@ -852,7 +869,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true}); expect(r).toEqual({file: INTEGER_STRING}); }); @@ -863,7 +880,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true}); expect(r).toEqual({password: INTEGER_STRING}); }); @@ -875,7 +892,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true}); expect(r).toEqual({search: INTEGER_STRING}); }); @@ -887,7 +904,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true}); expect(r).toEqual({url: INTEGER_STRING}); }); @@ -898,7 +915,7 @@ describe("parseForm()", () => { value: INTEGER_STRING })); - const r = FormUtils.parseForm(form, {parseValues: true}); + const r = FormUtils.parseForm(form, {dynamicTyping: true}); expect(r).toEqual({textarea: INTEGER_STRING}); }); });