Skip to content
This repository has been archived by the owner on Apr 16, 2018. It is now read-only.

Commit

Permalink
Added transformObjectsToLocalDates() function (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkadept authored Aug 22, 2017
1 parent 9fb32ec commit 2f269b1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
src/
lib/
docs/
coverage/
10 changes: 10 additions & 0 deletions lib/__tests__/__snapshots__/util.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ Object {
}
`;

exports[`Date transforms should transform objects to LocalDates 1`] = `
Object {
"field1": "2017-08-22",
"field2": Array [
"2005-08-05",
"1970-01-02",
],
}
`;

exports[`Formatting should format with defaults 1`] = `"6/23/2017"`;

exports[`Formatting should format with options 1`] = `"6/23/2017 6:00 am"`;
Expand Down
11 changes: 11 additions & 0 deletions lib/__tests__/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
transformLocalDatesToEpochInteger,
transformEpochIntegerToLocalDate,
mapEpochIntegerToLocalDates,
transformObjectsToLocalDates,
formatDate,
} from '../util';

Expand Down Expand Up @@ -170,6 +171,16 @@ describe('Date transforms', () => {
expect(val.field1).toBeInstanceOf(LocalDate);
expect(val).toMatchSnapshot();
});

it('should transform objects to LocalDates', () => {
const val = transformObjectsToLocalDates({
field1: LocalDate.ofEpochDay(17400),
field2: [LocalDate.ofEpochDay(13000), LocalDate.ofEpochDay(1)],
});
expect(val.field1).toBeInstanceOf(LocalDate);
expect(val.field2[0]).toBeInstanceOf(LocalDate);
expect(val).toMatchSnapshot();
});
});

describe('Formatting', () => {
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
transformLocalDatesToEpochInteger,
transformEpochIntegerToLocalDate,
mapEpochIntegerToLocalDates,
transformObjectsToLocalDates,
dateInit,
} from './util';

Expand All @@ -32,4 +33,5 @@ export {
transformLocalDatesToEpochInteger,
transformEpochIntegerToLocalDate,
mapEpochIntegerToLocalDates,
transformObjectsToLocalDates
};
13 changes: 13 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ export function mapEpochIntegerToLocalDates(obj, paths, curPath = []) {
return obj;
}

export function transformObjectsToLocalDates(obj) {
if (isArray(obj)) {
return map(obj, v => transformObjectsToLocalDates(v));
}
if (isObject(obj)) {
if (obj._year && obj._month && obj._day) return LocalDate.of(obj._year, obj._month, obj._day) // eslint-disable-line no-underscore-dangle
return transform(obj, (result, value, key) => {
result[key] = transformObjectsToLocalDates(value); // eslint-disable-line no-param-reassign
}, {});
}
return obj;
}


/**
* Formats a date to a predefined style
Expand Down

0 comments on commit 2f269b1

Please sign in to comment.