Skip to content

Commit ed1abb3

Browse files
authored
Merge branch 'master' into master
2 parents 661a8c4 + 20b9752 commit ed1abb3

6 files changed

Lines changed: 55 additions & 15 deletions

File tree

dist/mobx-spine.cjs.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,11 +2213,11 @@ function checkMomentInstance(attr, value) {
22132213
}
22142214

22152215
function checkLuxonDateTime(attr, value) {
2216-
invariant(moment.isMoment(value), 'Attribute `' + attr + '` is not a luxon DateTime.');
2216+
invariant(luxon.DateTime.isDateTime(value), 'Attribute `' + attr + '` is not a luxon instance.');
22172217
}
22182218

22192219
var LUXON_DATE_FORMAT = 'yyyy-LL-dd';
2220-
var LUXON_DATETIME_FORMAT = 'yyy-LL-ddTHH:mm:ssZZZ';
2220+
var LUXON_DATETIME_FORMAT = "yyyy'-'LL'-'dd'T'HH':'mm':'ssZZ";
22212221

22222222
var CASTS = {
22232223
momentDate: {
@@ -2259,7 +2259,7 @@ var CASTS = {
22592259
if (value === null || value === undefined) {
22602260
return null;
22612261
}
2262-
return luxon.DateTime.fromFormat(value, LUXON_DATE_FORMAT);
2262+
return luxon.DateTime.fromISO(value);
22632263
},
22642264
toJS: function toJS(attr, value) {
22652265
if (value === null || value === undefined) {
@@ -2276,7 +2276,8 @@ var CASTS = {
22762276
if (value === null) {
22772277
return null;
22782278
}
2279-
return luxon.DateTime.fromFormat(value, LUXON_DATETIME_FORMAT);
2279+
2280+
return luxon.DateTime.fromISO(value);
22802281
},
22812282
toJS: function toJS(attr, value) {
22822283
if (value === null) {

dist/mobx-spine.es.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,11 +2207,11 @@ function checkMomentInstance(attr, value) {
22072207
}
22082208

22092209
function checkLuxonDateTime(attr, value) {
2210-
invariant(moment.isMoment(value), 'Attribute `' + attr + '` is not a luxon DateTime.');
2210+
invariant(DateTime.isDateTime(value), 'Attribute `' + attr + '` is not a luxon instance.');
22112211
}
22122212

22132213
var LUXON_DATE_FORMAT = 'yyyy-LL-dd';
2214-
var LUXON_DATETIME_FORMAT = 'yyy-LL-ddTHH:mm:ssZZZ';
2214+
var LUXON_DATETIME_FORMAT = "yyyy'-'LL'-'dd'T'HH':'mm':'ssZZ";
22152215

22162216
var CASTS = {
22172217
momentDate: {
@@ -2253,7 +2253,7 @@ var CASTS = {
22532253
if (value === null || value === undefined) {
22542254
return null;
22552255
}
2256-
return DateTime.fromFormat(value, LUXON_DATE_FORMAT);
2256+
return DateTime.fromISO(value);
22572257
},
22582258
toJS: function toJS$$1(attr, value) {
22592259
if (value === null || value === undefined) {
@@ -2270,7 +2270,8 @@ var CASTS = {
22702270
if (value === null) {
22712271
return null;
22722272
}
2273-
return DateTime.fromFormat(value, LUXON_DATETIME_FORMAT);
2273+
2274+
return DateTime.fromISO(value);
22742275
},
22752276
toJS: function toJS$$1(attr, value) {
22762277
if (value === null) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobx-spine",
3-
"version": "0.28.3",
3+
"version": "0.28.1",
44
"license": "ISC",
55
"author": "Kees Kluskens <kees@webduck.nl>",
66
"description": "MobX with support for models, relations and an API.",

src/Casts.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ function checkMomentInstance(attr, value) {
2424

2525
function checkLuxonDateTime(attr, value) {
2626
invariant(
27-
moment.isMoment(value),
28-
`Attribute \`${attr}\` is not a luxon DateTime.`
27+
DateTime.isDateTime(value),
28+
`Attribute \`${attr}\` is not a luxon instance.`
2929
);
3030
}
3131

3232
const LUXON_DATE_FORMAT = 'yyyy-LL-dd';
33-
const LUXON_DATETIME_FORMAT = 'yyy-LL-ddTHH:mm:ssZZZ';
33+
const LUXON_DATETIME_FORMAT = "yyyy'-'LL'-'dd'T'HH':'mm':'ssZZ";
3434

3535
const CASTS = {
3636
momentDate: {
@@ -70,7 +70,7 @@ const CASTS = {
7070
if (value === null || value === undefined) {
7171
return null;
7272
}
73-
return DateTime.fromFormat(value, LUXON_DATE_FORMAT);
73+
return DateTime.fromISO(value);
7474
},
7575
toJS(attr, value) {
7676
if (value === null || value === undefined) {
@@ -86,7 +86,8 @@ const CASTS = {
8686
if (value === null) {
8787
return null;
8888
}
89-
return DateTime.fromFormat(value, LUXON_DATETIME_FORMAT);
89+
90+
return DateTime.fromISO(value);
9091
},
9192
toJS(attr, value) {
9293
if (value === null) {

src/__tests__/Casts/datetime.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Model, Casts } from '../../';
22
import { observable } from 'mobx';
3+
import { Settings } from 'luxon';
34
import moment from 'moment';
45
import momentLocale from 'moment/min/moment-with-locales';
56

@@ -79,3 +80,39 @@ test('moment instance with locale should be recognized', () => {
7980
animal.bornAt = momentLocale('2017-03-22T22:08:23+00:00');
8081
expect(animal.toJS().bornAt).toEqual(expect.stringContaining('2017-03-22'));
8182
});
83+
84+
describe('luxon compatibility', () => {
85+
Settings.defaultZoneName = 'utc';
86+
87+
class LuxonAnimal extends Animal {
88+
@observable createdAt = '';
89+
90+
casts() {
91+
return {
92+
bornAt: Casts.luxonDatetime,
93+
};
94+
}
95+
};
96+
97+
test('toJS() should throw error when luxon instance is gone', () => {
98+
const animal = new LuxonAnimal({ bornAt: '2017-03-22T22:08:23+00:00' });
99+
100+
animal.bornAt = 'asdf';
101+
102+
expect(() => {
103+
return animal.toJS();
104+
}).toThrow('Attribute `bornAt` is not a luxon instance.');
105+
});
106+
107+
test('should be serialized in toBackend()', () => {
108+
const animal = new LuxonAnimal({ bornAt: '2017-03-22T22:08:23+00:00' });
109+
110+
expect(animal.toBackend().born_at).toEqual('2017-03-22T22:08:23+00:00');
111+
});
112+
113+
test('should be serialized in toBackend() when given a binder specific format', () => {
114+
const animal = new LuxonAnimal({ bornAt: '2017-03-22T22:08:23.575242+0000' });
115+
116+
expect(animal.toBackend().born_at).toEqual('2017-03-22T22:08:23+00:00');
117+
});
118+
});

src/__tests__/Model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import axios from 'axios';
22
import { toJS, observable } from 'mobx';
33
import MockAdapter from 'axios-mock-adapter';
44
import _ from 'lodash';
5-
import { Model, BinderApi } from '../';
5+
import { Model, BinderApi, Casts } from '../';
66
import { compareObjectsIgnoringNegativeIds } from "./helpers";
77
import {
88
Animal,

0 commit comments

Comments
 (0)