Skip to content

feat: add class manipulation via functions #12

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-plugin-import": "^2.24.0",
"jest": "^27.0.6",
"prettier": "^2.5.1",
"release-it": "^14.11.3",
"ts-jest": "^27.0.4",
"typescript": "^4.4.3",
Expand Down
11 changes: 7 additions & 4 deletions src/__tests__/addToArray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ describe('addToArray', () => {

it('adds a new item for objects', () => {
const arr = [{ a: 1 }, { b: '2' }, { three: '3' }];
expect(addToArray(arr, { 'some-thing': { test: 1 } }))
.toEqual([{ a: 1 }, { b: '2' }, { three: '3' }, { 'some-thing': { test: 1 } }]);
expect(addToArray(arr, { 'some-thing': { test: 1 } })).toEqual([
{ a: 1 },
{ b: '2' },
{ three: '3' },
{ 'some-thing': { test: 1 } },
]);
});

it('returns an array with the item if parameter is not an array', () => {
expect(addToArray(null, 'whatever'))
.toEqual(['whatever']);
expect(addToArray(null, 'whatever')).toEqual(['whatever']);
});
});
19 changes: 12 additions & 7 deletions src/__tests__/clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ describe('clone', () => {
});

it('makes a deep clone', () => {
const obj = [{
a: 1,
test: 2,
something: {
const obj = [
{
a: 1,
hola: 'Mundo',
'an-array': [1, { hello: 'wolrd', test: { foo: '1' } }, ['a', 'b', 'C']],
test: 2,
something: {
a: 1,
hola: 'Mundo',
'an-array': [1, { hello: 'wolrd', test: { foo: '1' } }, ['a', 'b', 'C']],
},
},
}, null, [], { a: 1, b: 2, c: 3 }];
null,
[],
{ a: 1, b: 2, c: 3 },
];

expect(clone(obj)).toEqual(obj);
});
Expand Down
18 changes: 3 additions & 15 deletions src/__tests__/dates/dayIsPartOfTheConditions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,21 @@ describe('dayIsPartOfTheConditions', () => {

it('handles an array of conditions', () => {
const date = new Date(2019, 0, 1);
const conditions = [
(d: Date) => d.getFullYear() === 2019,
'2019-01-01',
new Date(2019, 0, 1),
];
const conditions = [(d: Date) => d.getFullYear() === 2019, '2019-01-01', new Date(2019, 0, 1)];

expect(dayIsPartOfTheConditions(date, conditions, dateParser, 'Y-m-d')).toBe(true);
});

it('handles an array of conditions if one condition is false', () => {
const date = new Date(2019, 0, 1);
const conditions = [
(d: Date) => d.getFullYear() !== 2019,
'2019-01-01',
new Date(2019, 0, 1),
];
const conditions = [(d: Date) => d.getFullYear() !== 2019, '2019-01-01', new Date(2019, 0, 1)];

expect(dayIsPartOfTheConditions(date, conditions, dateParser, 'Y-m-d')).toBe(true);
});

it('handles an array of conditions if all condition are false', () => {
const date = new Date(2019, 0, 1);
const conditions = [
(d: Date) => d.getFullYear() !== 2019,
'2019-02-01',
new Date(2010, 0, 1),
];
const conditions = [(d: Date) => d.getFullYear() !== 2019, '2019-02-01', new Date(2010, 0, 1)];

expect(dayIsPartOfTheConditions(date, conditions, dateParser, 'Y-m-d')).toBe(false);
});
Expand Down
27 changes: 25 additions & 2 deletions src/__tests__/dates/formatDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,28 @@ describe('formatDate', () => {

describe('Custom locale', () => {
const allTokens = [
'D', 'F', 'G', 'H', 'J', 'K', 'M', 'S', 'U', 'W', 'Y', 'Z', 'd', 'h', 'i', 'j', 'l', 'm', 'n', 's', 'w', 'y',
'D',
'F',
'G',
'H',
'J',
'K',
'M',
'S',
'U',
'W',
'Y',
'Z',
'd',
'h',
'i',
'j',
'l',
'm',
'n',
's',
'w',
'y',
];

it('Spanish', () => {
Expand All @@ -160,7 +181,9 @@ describe('formatDate', () => {
...defaultLocale,
...Spanish,
};
expect(formatDate(date, allTokens.join('-'), customLocale)).toEqual('Vie-Julio-08-08-2º-AM-Jul-08-1625231468-26-2021-2021-07-02T13:11:08.000Z-02-8-11-2-Viernes-07-7-8-5-21');
expect(formatDate(date, allTokens.join('-'), customLocale)).toEqual(
'Vie-Julio-08-08-2º-AM-Jul-08-1625231468-26-2021-2021-07-02T13:11:08.000Z-02-8-11-2-Viernes-07-7-8-5-21',
);
});
});
});
36 changes: 25 additions & 11 deletions src/__tests__/dates/parseDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,44 @@ describe('parseDate', () => {
});

it('returns today date if passed `today`', () => {
const today = new Date(new Date().getFullYear(), (new Date()).getMonth(), (new Date()).getDate(), 0, 0, 0, 0);
const today = new Date(
new Date().getFullYear(),
new Date().getMonth(),
new Date().getDate(),
0,
0,
0,
0,
);
expect(parseDate('today')).toEqual(today);
});

it('parses gmt dates', () => {
expect(parseDate('Sat, 23 Oct 2021 20:58:00 GMT'))
.toEqual(new Date(Date.UTC(2021, 9, 23, 20, 58, 0, 0)));
expect(parseDate('Sat, 23 Oct 2021 20:58:00 GMT')).toEqual(
new Date(Date.UTC(2021, 9, 23, 20, 58, 0, 0)),
);
});

it('parses iso dates', () => {
expect(parseDate('2021-10-23T20:58:11.733Z'))
.toEqual(new Date(Date.UTC(2021, 9, 23, 20, 58, 11, 733)));
expect(parseDate('2021-10-23T20:58:11.733Z')).toEqual(
new Date(Date.UTC(2021, 9, 23, 20, 58, 11, 733)),
);
});

it('parses a date in the default format', () => {
expect(parseDate('2020-02-18 12:34:56')).toEqual(new Date(2020, 1, 18, 12, 34, 56));
});

it('parses a date ignoring time', () => {
expect(parseDate('2020-02-18 12:34:56', defaultFormat, timeless)).toEqual(new Date(2020, 1, 18, 0, 0, 0));
expect(parseDate('2020-02-18 12:34:56', defaultFormat, timeless)).toEqual(
new Date(2020, 1, 18, 0, 0, 0),
);
});

it('escapes the token', () => {
expect(parseDate('2020Y-02-18 12m:34:56', 'Y\\Y-m-d H\\m:i:S')).toEqual(new Date(2020, 1, 18, 12, 34, 56));
expect(parseDate('2020Y-02-18 12m:34:56', 'Y\\Y-m-d H\\m:i:S')).toEqual(
new Date(2020, 1, 18, 12, 34, 56),
);
});

it('returns undefined if using an invalid format', () => {
Expand All @@ -66,9 +80,7 @@ describe('parseDate', () => {
beforeEach(() => {
baseDate = new Date('2021-01-01T06:00:00.000Z');

jest
.useFakeTimers()
.setSystemTime(baseDate.getTime());
jest.useFakeTimers().setSystemTime(baseDate.getTime());
});

afterEach(() => {
Expand Down Expand Up @@ -141,7 +153,9 @@ describe('parseDate', () => {
});
// Z / ISO Date format / 2017-03-04T01:23:43.000Z
it('Z', () => {
expect(parseDate('2020-01-01T06:00:00.000', 'Z')).toEqual(new Date('2020-01-01T12:00:00.000Z'));
expect(parseDate('2020-01-01T06:00:00.000', 'Z')).toEqual(
new Date('2020-01-01T12:00:00.000Z'),
);
});

// H / Hours (24 hours) / 00 to 23
Expand Down
4 changes: 1 addition & 3 deletions src/__tests__/filterOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ describe('filterOptions', () => {
{
value: 1,
text: 'Option 1',
children: [
{ value: 'red', text: 'Red' },
],
children: [{ value: 'red', text: 'Red' }],
},
],
},
Expand Down
9 changes: 1 addition & 8 deletions src/__tests__/getFocusableElements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,7 @@ describe('elementIsTargetOrTargetChild', () => {
it('doesnt return any element that is disabled ', () => {
const el = document.createElement('div');

const els = [
'a',
'button',
'input',
'textarea',
'select',
'details',
];
const els = ['a', 'button', 'input', 'textarea', 'select', 'details'];

els.forEach((tagName) => {
const focusable = document.createElement(tagName);
Expand Down
94 changes: 61 additions & 33 deletions src/__tests__/isEqual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ describe('isEqual', () => {
});

it('considers that an array with same values to be equal', () => {
expect(isEqual([1, '12', 'string', true, undefined], [1, '12', 'string', true, undefined])).toBe(true);
expect(
isEqual([1, '12', 'string', true, undefined], [1, '12', 'string', true, undefined]),
).toBe(true);
});

it('considers that an array with same values in different order to no be equal', () => {
expect(isEqual([1, '12', 'string', true, undefined], ['12', 1, 'string', true, undefined])).toBe(false);
expect(
isEqual([1, '12', 'string', true, undefined], ['12', 1, 'string', true, undefined]),
).toBe(false);
});

it('considers that `undefined` is equal to `undefined`', () => {
Expand Down Expand Up @@ -102,50 +106,74 @@ describe('isEqual', () => {
});

it('makes a deep comparison', () => {
const a = [undefined, {
a: 1,
test: 2,
something: {
const a = [
undefined,
{
a: 1,
hola: 'Mundo',
'an-array': [1, undefined, { hello: 'wolrd', test: { foo: '1' } }, ['a', 'b', 'C']],
test: 2,
something: {
a: 1,
hola: 'Mundo',
'an-array': [1, undefined, { hello: 'wolrd', test: { foo: '1' } }, ['a', 'b', 'C']],
},
},
}, null, [], { a: 1, b: 2, c: 3 }];

const b = [undefined, {
a: 1,
test: 2,
something: {
null,
[],
{ a: 1, b: 2, c: 3 },
];

const b = [
undefined,
{
a: 1,
hola: 'Mundo',
'an-array': [1, undefined, { hello: 'wolrd', test: { foo: '1' } }, ['a', 'b', 'C']],
test: 2,
something: {
a: 1,
hola: 'Mundo',
'an-array': [1, undefined, { hello: 'wolrd', test: { foo: '1' } }, ['a', 'b', 'C']],
},
},
}, null, [], { a: 1, b: 2, c: 3 }];
null,
[],
{ a: 1, b: 2, c: 3 },
];

expect(isEqual(a, b)).toBe(true);
});

it('makes a deep comparison for something that is not equal', () => {
const a = [undefined, {
a: 1,
test: 2,
something: {
const a = [
undefined,
{
a: 1,
hola: 'Mundo',
'an-array': [1, undefined, { hello: 'wolrd', test: { foo: '1' } }, ['a', 'b', 'C']],
test: 2,
something: {
a: 1,
hola: 'Mundo',
'an-array': [1, undefined, { hello: 'wolrd', test: { foo: '1' } }, ['a', 'b', 'C']],
},
},
}, null, [], { a: 1, b: 2, c: 3 }];

const b = [undefined, {
a: 1,
test: 2,
something: {
null,
[],
{ a: 1, b: 2, c: 3 },
];

const b = [
undefined,
{
a: 1,
hola: 'Mundo',
// The 1 in foo is different
'an-array': [1, undefined, { hello: 'wolrd', test: { foo: 1 } }, ['a', 'b', 'C']],
test: 2,
something: {
a: 1,
hola: 'Mundo',
// The 1 in foo is different
'an-array': [1, undefined, { hello: 'wolrd', test: { foo: 1 } }, ['a', 'b', 'C']],
},
},
}, null, [], { a: 1, b: 2, c: 3 }];
null,
[],
{ a: 1, b: 2, c: 3 },
];

expect(isEqual(a, b)).toBe(false);
});
Expand Down
23 changes: 22 additions & 1 deletion src/__tests__/mergeClasses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ describe('merge classes function', () => {
expect(mergeClasses(['hello'], ['world'])).toBe('hello world');
});

it('allows functions that can manipulate the classes interactively', () => {
expect(
mergeClasses(
['hello'],
({ clear, add }) => {
clear();
add('no');
},
['world'],
({ remove }) => {
remove('world');
},
),
).toBe('no');
});

it('does not allowe duplicates', () => {
expect(mergeClasses(['hello'], ['hello'])).toBe('hello');
});

it('merges the truthy values from an object format', () => {
expect(
mergeClasses(
Expand All @@ -23,7 +43,8 @@ describe('merge classes function', () => {
{
world: 1,
universe: null,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
),
).toBe('hello world');
});
Expand Down
Loading