Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from hckrnews/feature/cap_all_words
Capitalize All Words
- Loading branch information
Showing
6 changed files
with
680 additions
and
633 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,87 @@ | ||
import { expect, describe, it } from '@jest/globals' | ||
import DefaultMutator from '../default.js' | ||
import { expect, describe, it } from '@jest/globals'; | ||
import DefaultMutator from '../default.js'; | ||
|
||
class ExampleMutator extends DefaultMutator { | ||
setSkuAttribute (sku) { | ||
this.sku = `*${sku}*` | ||
setSkuAttribute(sku) { | ||
this.sku = `*${sku}*`; | ||
} | ||
} | ||
|
||
setProductGroupAttribute(productGroup) { | ||
this.product ??= { group: null }; | ||
this.product.group = `*${productGroup}*`; | ||
} | ||
} | ||
|
||
describe('Test the filter mutator', () => { | ||
it('It should skip empty values', () => { | ||
const result = DefaultMutator.create({ test: 'ok', test2: null, test3: undefined, test4: NaN, test5: false, test6: 0, test7: 1 }) | ||
expect({ ...result }).toEqual({ test: 'ok', test5: false, test6: 0, test7: 1 }) | ||
}) | ||
|
||
it('It should set the item', () => { | ||
const result = ExampleMutator.create({ sku: 42, test: 'ok' }) | ||
expect({ ...result }).toEqual({ sku: '*42*', test: 'ok' }) | ||
}) | ||
|
||
it('It should only call a setter', () => { | ||
const result = ExampleMutator.create({ sku: 42 }) | ||
expect({ ...result }).toEqual({ sku: '*42*' }) | ||
}) | ||
|
||
it('It should not call a setter', () => { | ||
const result = ExampleMutator.create({ test: 'ok', test2: 'also ok' }) | ||
expect({ ...result }).toEqual({ test: 'ok', test2: 'also ok' }) | ||
}) | ||
|
||
it('It should hydrate the object with new data', () => { | ||
const result = ExampleMutator.create({ test: 'ok', test2: 'also ok' }) | ||
expect({ ...result }).toEqual({ test: 'ok', test2: 'also ok' }) | ||
result.hydrate({ sku: 43}) | ||
expect({ ...result }).toEqual({ sku: '*43*', test: 'ok', test2: 'also ok' }) | ||
result.hydrate({ test: 'another text'}) | ||
expect({ ...result }).toEqual({ sku: '*43*', test: 'another text', test2: 'also ok' }) | ||
}) | ||
|
||
it('It should set data', () => { | ||
const result = ExampleMutator.create({ test: 'ok', test2: 'also ok' }) | ||
expect({ ...result }).toEqual({ test: 'ok', test2: 'also ok' }) | ||
result.setter(['sku', 43]) | ||
expect({ ...result }).toEqual({ sku: '*43*', test: 'ok', test2: 'also ok' }) | ||
result.setter(['test', 'another text']) | ||
expect({ ...result }).toEqual({ sku: '*43*', test: 'another text', test2: 'also ok' }) | ||
}) | ||
}) | ||
it('It should skip empty values', () => { | ||
const result = DefaultMutator.create({ | ||
test: 'ok', | ||
test2: null, | ||
test3: undefined, | ||
test4: NaN, | ||
test5: false, | ||
test6: 0, | ||
test7: 1, | ||
}); | ||
expect({ ...result }).toEqual({ | ||
test: 'ok', | ||
test5: false, | ||
test6: 0, | ||
test7: 1, | ||
}); | ||
}); | ||
|
||
it('It should set the item', () => { | ||
const result = ExampleMutator.create({ sku: 42, test: 'ok' }); | ||
expect({ ...result }).toEqual({ sku: '*42*', test: 'ok' }); | ||
}); | ||
|
||
it('It should only call a setter', () => { | ||
const result = ExampleMutator.create({ sku: 42 }); | ||
expect({ ...result }).toEqual({ sku: '*42*' }); | ||
}); | ||
|
||
it('It should handle the product_group', () => { | ||
const result = ExampleMutator.create({ product_group: 'test' }); | ||
expect({ ...result }).toEqual({ product: { group: '*test*' } }); | ||
}); | ||
|
||
it('It should not call a setter', () => { | ||
const result = ExampleMutator.create({ test: 'ok', test2: 'also ok' }); | ||
expect({ ...result }).toEqual({ test: 'ok', test2: 'also ok' }); | ||
}); | ||
|
||
it('It should hydrate the object with new data', () => { | ||
const result = ExampleMutator.create({ test: 'ok', test2: 'also ok' }); | ||
expect({ ...result }).toEqual({ test: 'ok', test2: 'also ok' }); | ||
result.hydrate({ sku: 43 }); | ||
expect({ ...result }).toEqual({ | ||
sku: '*43*', | ||
test: 'ok', | ||
test2: 'also ok', | ||
}); | ||
result.hydrate({ test: 'another text' }); | ||
expect({ ...result }).toEqual({ | ||
sku: '*43*', | ||
test: 'another text', | ||
test2: 'also ok', | ||
}); | ||
}); | ||
|
||
it('It should set data', () => { | ||
const result = ExampleMutator.create({ test: 'ok', test2: 'also ok' }); | ||
expect({ ...result }).toEqual({ test: 'ok', test2: 'also ok' }); | ||
result.setter(['sku', 43]); | ||
expect({ ...result }).toEqual({ | ||
sku: '*43*', | ||
test: 'ok', | ||
test2: 'also ok', | ||
}); | ||
result.setter(['test', 'another text']); | ||
expect({ ...result }).toEqual({ | ||
sku: '*43*', | ||
test: 'another text', | ||
test2: 'also ok', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { expect, describe, it } from '@jest/globals'; | ||
import { capitalizeWords } from '../string-helper.js'; | ||
|
||
const testCases = [ | ||
{ | ||
description: 'It should capitalize words', | ||
input: 'test', | ||
expectedResult: 'Test', | ||
}, | ||
{ | ||
description: 'It should capitalize "test_test_test"', | ||
input: 'test_test_test', | ||
expectedResult: 'TestTestTest', | ||
}, | ||
{ | ||
description: 'It should capitalize "test test test"', | ||
input: 'test test test', | ||
expectedResult: 'TestTestTest', | ||
}, | ||
{ | ||
description: 'It should capitalize "tEst teSt tesT"', | ||
input: 'tEst teSt tesT', | ||
expectedResult: 'TEstTeStTesT', | ||
}, | ||
]; | ||
|
||
describe.each(testCases)( | ||
'Test the item filter method', | ||
({ description, input, expectedResult }) => { | ||
it(description, () => { | ||
const result = capitalizeWords(input); | ||
expect(result).toEqual(expectedResult); | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export const capitalizeFirstLetter = (string) => | ||
string.charAt(0).toUpperCase() + string.slice(1); | ||
export const capitalizeWords = (string) => | ||
string | ||
.split(/_| /) | ||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1)) | ||
.join(''); |