From c35990fc21adefb4e3212c7b5296d22160daf9a0 Mon Sep 17 00:00:00 2001 From: Yusuf Khamis Date: Wed, 18 Aug 2021 12:27:39 +0300 Subject: [PATCH 1/5] added normalize transform option to normalize string spaces --- README.md | 1 + spec/transform.spec.ts | 26 ++++++++++++++++++++++++++ src/definitions/transform.ts | 2 ++ 3 files changed, 29 insertions(+) diff --git a/README.md b/README.md index d0a8f45..d68200d 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,7 @@ A standalone string cannot be modified, i.e. `data = 'a'; ajv.validate(schema, d - `toLowerCase`: convert to lower case - `toUpperCase`: convert to upper case - `toEnumCase`: change string case to be equal to one of `enum` values in the schema +- `normalize`: replace inner multiple spaces with a single space Transformations are applied in the order they are listed. diff --git a/spec/transform.spec.ts b/spec/transform.spec.ts index 8aa5d27..6a3ea34 100644 --- a/spec/transform.spec.ts +++ b/spec/transform.spec.ts @@ -129,4 +129,30 @@ describe('keyword "transform"', () => { data.should.deep.equal(["ab"]) }) }) + + ajvs.forEach((ajv, i) => { + it(`should transform normalize #${i}`, () => { + let schema, data + + data = [" normalize object to test "] + schema = {type: "array", items: {type: "string", transform: ["normalize"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal([" normalize object to test "]) + + data = ["normalize"] + schema = {type: "array", items: {type: "string", transform: ["normalize"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal(["normalize"]) + + data = ["normalize object to test"] + schema = {type: "array", items: {type: "string", transform: ["normalize"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal(["normalize object to test"]) + + data = ["normalize object to test"] + schema = {type: "array", items: {type: "string", transform: ["normalize"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal(["normalize object to test"]) + }) + }) }) diff --git a/src/definitions/transform.ts b/src/definitions/transform.ts index beeddb9..378cbbf 100644 --- a/src/definitions/transform.ts +++ b/src/definitions/transform.ts @@ -10,6 +10,7 @@ type TransformName = | "toLowerCase" | "toUpperCase" | "toEnumCase" + | "normalize" interface TransformConfig { hash: Record @@ -26,6 +27,7 @@ const transform: {[key in TransformName]: Transform} = { toLowerCase: (s) => s.toLowerCase(), toUpperCase: (s) => s.toUpperCase(), toEnumCase: (s, cfg) => cfg?.hash[configKey(s)] || s, + normalize: (s) => s.replace(/\s\s+/g, " "), } const getDef: (() => CodeKeywordDefinition) & { From d7be11c05229fc73f1d0d37feaad6b26394c3f8b Mon Sep 17 00:00:00 2001 From: Yusuf Khamis Date: Mon, 23 Aug 2021 14:38:29 +0300 Subject: [PATCH 2/5] fixed regex for normalizeSpaces, renamed normalize to normalizeSpaces and added trimInner option --- README.md | 3 +- spec/transform.spec.ts | 71 +++++++++++++++++++++++++++++++++--- src/definitions/transform.ts | 9 ++++- 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d68200d..2d1ad1e 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,8 @@ A standalone string cannot be modified, i.e. `data = 'a'; ajv.validate(schema, d - `toLowerCase`: convert to lower case - `toUpperCase`: convert to upper case - `toEnumCase`: change string case to be equal to one of `enum` values in the schema -- `normalize`: replace inner multiple spaces with a single space +- `normalizeSpaces`: replace all multiple white spaces with a single white space in a string +- `trimInner`: remove all white spaces only inside/within string Transformations are applied in the order they are listed. diff --git a/spec/transform.spec.ts b/spec/transform.spec.ts index 6a3ea34..39d02f3 100644 --- a/spec/transform.spec.ts +++ b/spec/transform.spec.ts @@ -131,28 +131,89 @@ describe('keyword "transform"', () => { }) ajvs.forEach((ajv, i) => { - it(`should transform normalize #${i}`, () => { + it(`should transform normalizeSpaces #${i}`, () => { let schema, data data = [" normalize object to test "] - schema = {type: "array", items: {type: "string", transform: ["normalize"]}} + schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}} ajv.validate(schema, data).should.equal(true) data.should.deep.equal([" normalize object to test "]) data = ["normalize"] - schema = {type: "array", items: {type: "string", transform: ["normalize"]}} + schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}} ajv.validate(schema, data).should.equal(true) data.should.deep.equal(["normalize"]) data = ["normalize object to test"] - schema = {type: "array", items: {type: "string", transform: ["normalize"]}} + schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}} ajv.validate(schema, data).should.equal(true) data.should.deep.equal(["normalize object to test"]) data = ["normalize object to test"] - schema = {type: "array", items: {type: "string", transform: ["normalize"]}} + schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}} ajv.validate(schema, data).should.equal(true) data.should.deep.equal(["normalize object to test"]) + + data = [ + `A tab\t\tanother multiple tabs\t\t\t\tnew line +multiple new lines + + + +Multiple spaces end`, + ] + schema = {type: "array", items: {type: "string", transform: ["normalizeSpaces"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal([ + "A tab another multiple tabs new line multiple new lines Multiple spaces end", + ]) + }) + }) + + ajvs.forEach((ajv, i) => { + it(`should transform trimInner #${i}`, () => { + let schema, data + + data = [" trimInner object to test "] + schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal([" trimInnerobjecttotest "]) + + data = [ + ` + trimInner + multiple spaces + `, + ] + schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal([ + ` + trimInnermultiplespaces + `, + ]) + + data = ["trimInner \twith\t\t\ttabs\t\t\tinside"] + schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal(["trimInnerwithtabsinside"]) + + data = [" trimInner object to test with multiple \t\t spaces \t"] + schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal([" trimInnerobjecttotestwithmultiplespaces \t"]) + + data = [ + `A tab\t\tanother multiple tabs\t\t\t\tnew line +multiple new lines + + + +Multiple spaces end`, + ] + schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} + ajv.validate(schema, data).should.equal(true) + data.should.deep.equal(["AtabanothermultipletabsnewlinemultiplenewlinesMultiplespacesend"]) }) }) }) diff --git a/src/definitions/transform.ts b/src/definitions/transform.ts index 378cbbf..1e28d7a 100644 --- a/src/definitions/transform.ts +++ b/src/definitions/transform.ts @@ -10,7 +10,8 @@ type TransformName = | "toLowerCase" | "toUpperCase" | "toEnumCase" - | "normalize" + | "normalizeSpaces" + | "trimInner" interface TransformConfig { hash: Record @@ -27,7 +28,11 @@ const transform: {[key in TransformName]: Transform} = { toLowerCase: (s) => s.toLowerCase(), toUpperCase: (s) => s.toUpperCase(), toEnumCase: (s, cfg) => cfg?.hash[configKey(s)] || s, - normalize: (s) => s.replace(/\s\s+/g, " "), + normalizeSpaces: (s) => s.replace(/\s\s+|\t+|\n+/g, " "), + trimInner: (s) => + (/^[\s\t\n]+/.exec(s)?.shift() || "") + + s.trim().replace(/[\s|\t|\n]+/g, "") + + (/[\s\t\n]+$/.exec(s)?.shift() || ""), } const getDef: (() => CodeKeywordDefinition) & { From 15d1763d4adeb492a60f84986a623ea10c3cc76d Mon Sep 17 00:00:00 2001 From: Yusuf Khamis Date: Mon, 23 Aug 2021 14:42:47 +0300 Subject: [PATCH 3/5] updated regex for normalizeSpaces --- src/definitions/transform.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/definitions/transform.ts b/src/definitions/transform.ts index 1e28d7a..b771d29 100644 --- a/src/definitions/transform.ts +++ b/src/definitions/transform.ts @@ -28,7 +28,7 @@ const transform: {[key in TransformName]: Transform} = { toLowerCase: (s) => s.toLowerCase(), toUpperCase: (s) => s.toUpperCase(), toEnumCase: (s, cfg) => cfg?.hash[configKey(s)] || s, - normalizeSpaces: (s) => s.replace(/\s\s+|\t+|\n+/g, " "), + normalizeSpaces: (s) => s.replace(/[\s\t\n]+/g, " "), trimInner: (s) => (/^[\s\t\n]+/.exec(s)?.shift() || "") + s.trim().replace(/[\s|\t|\n]+/g, "") + From 2103376ae241200b03e4f772f8ecfda454b5f198 Mon Sep 17 00:00:00 2001 From: Yusuf Khamis Date: Mon, 23 Aug 2021 14:45:08 +0300 Subject: [PATCH 4/5] fixed regex for trimInner --- src/definitions/transform.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/definitions/transform.ts b/src/definitions/transform.ts index b771d29..8202e7d 100644 --- a/src/definitions/transform.ts +++ b/src/definitions/transform.ts @@ -31,7 +31,7 @@ const transform: {[key in TransformName]: Transform} = { normalizeSpaces: (s) => s.replace(/[\s\t\n]+/g, " "), trimInner: (s) => (/^[\s\t\n]+/.exec(s)?.shift() || "") + - s.trim().replace(/[\s|\t|\n]+/g, "") + + s.trim().replace(/[\s\t\n]+/g, "") + (/[\s\t\n]+$/.exec(s)?.shift() || ""), } From 08fe5eb3da20b8c62da4364936b21acc31f47bee Mon Sep 17 00:00:00 2001 From: Yusuf Khamis Date: Mon, 23 Aug 2021 18:17:05 +0300 Subject: [PATCH 5/5] removed trimInner --- README.md | 1 - spec/transform.spec.ts | 47 ------------------------------------ src/definitions/transform.ts | 5 ---- 3 files changed, 53 deletions(-) diff --git a/README.md b/README.md index 2d1ad1e..6e6090b 100644 --- a/README.md +++ b/README.md @@ -221,7 +221,6 @@ A standalone string cannot be modified, i.e. `data = 'a'; ajv.validate(schema, d - `toUpperCase`: convert to upper case - `toEnumCase`: change string case to be equal to one of `enum` values in the schema - `normalizeSpaces`: replace all multiple white spaces with a single white space in a string -- `trimInner`: remove all white spaces only inside/within string Transformations are applied in the order they are listed. diff --git a/spec/transform.spec.ts b/spec/transform.spec.ts index 39d02f3..9de0132 100644 --- a/spec/transform.spec.ts +++ b/spec/transform.spec.ts @@ -169,51 +169,4 @@ Multiple spaces end`, ]) }) }) - - ajvs.forEach((ajv, i) => { - it(`should transform trimInner #${i}`, () => { - let schema, data - - data = [" trimInner object to test "] - schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} - ajv.validate(schema, data).should.equal(true) - data.should.deep.equal([" trimInnerobjecttotest "]) - - data = [ - ` - trimInner - multiple spaces - `, - ] - schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} - ajv.validate(schema, data).should.equal(true) - data.should.deep.equal([ - ` - trimInnermultiplespaces - `, - ]) - - data = ["trimInner \twith\t\t\ttabs\t\t\tinside"] - schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} - ajv.validate(schema, data).should.equal(true) - data.should.deep.equal(["trimInnerwithtabsinside"]) - - data = [" trimInner object to test with multiple \t\t spaces \t"] - schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} - ajv.validate(schema, data).should.equal(true) - data.should.deep.equal([" trimInnerobjecttotestwithmultiplespaces \t"]) - - data = [ - `A tab\t\tanother multiple tabs\t\t\t\tnew line -multiple new lines - - - -Multiple spaces end`, - ] - schema = {type: "array", items: {type: "string", transform: ["trimInner"]}} - ajv.validate(schema, data).should.equal(true) - data.should.deep.equal(["AtabanothermultipletabsnewlinemultiplenewlinesMultiplespacesend"]) - }) - }) }) diff --git a/src/definitions/transform.ts b/src/definitions/transform.ts index 8202e7d..6b973d5 100644 --- a/src/definitions/transform.ts +++ b/src/definitions/transform.ts @@ -11,7 +11,6 @@ type TransformName = | "toUpperCase" | "toEnumCase" | "normalizeSpaces" - | "trimInner" interface TransformConfig { hash: Record @@ -29,10 +28,6 @@ const transform: {[key in TransformName]: Transform} = { toUpperCase: (s) => s.toUpperCase(), toEnumCase: (s, cfg) => cfg?.hash[configKey(s)] || s, normalizeSpaces: (s) => s.replace(/[\s\t\n]+/g, " "), - trimInner: (s) => - (/^[\s\t\n]+/.exec(s)?.shift() || "") + - s.trim().replace(/[\s\t\n]+/g, "") + - (/[\s\t\n]+$/.exec(s)?.shift() || ""), } const getDef: (() => CodeKeywordDefinition) & {