Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added normalize transform option to normalize string spaces #217

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is too generic for what it does... Possible alternative - trimInner (and should it not replace tabs too?).

Any better ideas?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

word trim means to remove space, this just removes extra spaces, its not a bad idea though to also include remove spaces completely, with trimInner and come up with a better name for this one like normalizeSpaces

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's please not conflate two different things into on PR.

I don't know the use case for removing all inner spaces tbh, normalizeSpaces is ok though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will separate the other one in a different PR, will make another commit today

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review my latest commit


Transformations are applied in the order they are listed.

Expand Down
26 changes: 26 additions & 0 deletions spec/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
})
})
})
2 changes: 2 additions & 0 deletions src/definitions/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type TransformName =
| "toLowerCase"
| "toUpperCase"
| "toEnumCase"
| "normalize"

interface TransformConfig {
hash: Record<string, string | undefined>
Expand All @@ -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, " "),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just \s+ ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...or [\s\t]+ if tabs were to be included too

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because \s+ means single space or more, and in my case i want to only check multiple spaces and reduce them to one space

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, pretty much the regex \s\s+ catters for all spaces, tabs and line breaks, maybe i add more tests for these other scenarios, but will also improve the regex to /(\s\s+)|\t|\n/ for our own peace of mind

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The performance of replacing /\s+/ can be better than /\s\s+/ and the result will be exactly the same.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check my latest commits, i have resolved these issues and also added trimInner option

}

const getDef: (() => CodeKeywordDefinition) & {
Expand Down