Skip to content

Commit

Permalink
Merge pull request #4 from hckrnews/feature/cap_all_words
Browse files Browse the repository at this point in the history
Capitalize All Words
  • Loading branch information
w3nl committed Sep 7, 2021
2 parents 9d89710 + 6d03bbb commit cbde8c5
Show file tree
Hide file tree
Showing 6 changed files with 680 additions and 633 deletions.
1,140 changes: 555 additions & 585 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "@hckrnews/mutator",
"description": "Mutate the value when set some data",
"version": "0.1.1",
"version": "0.1.2",
"author": {
"name": "Pieter Wigboldus",
"url": "https://hckr.news/"
Expand Down
125 changes: 82 additions & 43 deletions src/__tests__/default.unit.js
@@ -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',
});
});
});
35 changes: 35 additions & 0 deletions src/__tests__/string-helper.unit.js
@@ -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);
});
}
);
4 changes: 2 additions & 2 deletions src/default.js
@@ -1,12 +1,12 @@
import { capitalizeFirstLetter } from './string-helper.js';
import { capitalizeWords } from './string-helper.js';

export default class DefaultMutator {
setter([key, value]) {
if (value === undefined || value === null || Number.isNaN(value)) {
return;
}

const fn = `set${capitalizeFirstLetter(key)}Attribute`;
const fn = `set${capitalizeWords(key)}Attribute`;
if (this?.[fn]?.constructor === Function) {
this[fn](value);
return;
Expand Down
7 changes: 5 additions & 2 deletions src/string-helper.js
@@ -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('');

0 comments on commit cbde8c5

Please sign in to comment.