From 339eac56600b564188f34406dae2d79d73a28308 Mon Sep 17 00:00:00 2001 From: Yusuf Khamis Date: Mon, 23 Aug 2021 18:24:25 +0300 Subject: [PATCH] added trimInner transform option --- README.md | 1 + spec/transform.spec.ts | 44 ++++++++++++++++++++++++++++++++++++ src/definitions/transform.ts | 5 ++++ 3 files changed, 50 insertions(+) diff --git a/README.md b/README.md index d0a8f45..43fb3b1 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 +- `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 8aa5d27..9a0e8a3 100644 --- a/spec/transform.spec.ts +++ b/spec/transform.spec.ts @@ -129,4 +129,48 @@ describe('keyword "transform"', () => { data.should.deep.equal(["ab"]) }) }) + + 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 beeddb9..6ba7818 100644 --- a/src/definitions/transform.ts +++ b/src/definitions/transform.ts @@ -10,6 +10,7 @@ type TransformName = | "toLowerCase" | "toUpperCase" | "toEnumCase" + | "trimInner" interface TransformConfig { hash: Record @@ -26,6 +27,10 @@ const transform: {[key in TransformName]: Transform} = { toLowerCase: (s) => s.toLowerCase(), toUpperCase: (s) => s.toUpperCase(), toEnumCase: (s, cfg) => cfg?.hash[configKey(s)] || s, + trimInner: (s) => + (/^[\s\t\n]+/.exec(s)?.shift() || "") + + s.trim().replace(/[\s\t\n]+/g, "") + + (/[\s\t\n]+$/.exec(s)?.shift() || ""), } const getDef: (() => CodeKeywordDefinition) & {