Permalink
Browse files

add basic Date deserialization support

  • Loading branch information...
bsatrom committed May 8, 2013
2 parents 0ea9508 + 8d8ab65 commit ec15352311f39594f2cc43d42d40db017e9293a8
Showing with 16 additions and 40 deletions.
  1. +1 −1 platform
  2. +1 −21 src/attrs.js
  3. +8 −12 test/html/take-attributes.html
  4. +2 −2 toolkit.min.js
  5. +1 −1 toolkit.min.js.map
  6. +2 −2 toolkit.native.min.js
  7. +1 −1 toolkit.native.min.js.map
View
@@ -96,15 +96,6 @@
return properties[properties.map(lowerCase).indexOf(name.toLowerCase())];
};
- function createDate(year, month, day, hour, minute, second, millisecond) {
- // hour through ms are optional and undefined if not provided. Passing
- // undefined into the Date constructor results in an invalid date.
- // Null, on the other hand, is valid.
- return new Date(year, month-1, day,
- hour ? hour : null, minute ? minute : null,
- second ? second : null, millisecond ? millisecond : null);
- };
-
function deserializeValue(inValue, inDefaultValue) {
var inferredType = typeof inDefaultValue;
if (inferredType === 'string') {
@@ -119,19 +110,8 @@
return false;
}
- // If the default attribute value is a Date, provide a Date object
- // for the specified value.
if (inDefaultValue instanceof Date) {
- // Strip the date string into its component parts and pass each into
- // the Date constructor.
- var dateParts = inValue.match(/(\d+)/g);
-
- // length < 3 means a text format was used (ex. March 20, 2010)
- if (dateParts.length >= 3) {
- return createDate.apply(null, dateParts);
- } else {
- return new Date(inValue);
- }
+ return new Date(Date.parse(inValue) || Date.now());
}
var float = parseFloat(inValue);
@@ -67,13 +67,11 @@
</script>
</element>
- <x-date id="date1" value="2014-12-25"></x-date>
- <x-date id="date2" value="2014/12/25"></x-date>
- <x-date id="date3" value="2014.12.25"></x-date>
- <x-date id="date4" value="December 25, 2014"></x-date>
- <x-date id="date5" value="2014-12-25 11:45"></x-date>
- <x-date id="date6" value="2014-12-25 11:45:30"></x-date>
- <x-date id="date7" value="2014-12-25 11:45:30.33"></x-date>
+ <x-date id="date1" value="2014/12/25"></x-date>
+ <x-date id="date2" value="December 25, 2014"></x-date>
+ <x-date id="date3" value="2014/12/25 11:45"></x-date>
+ <x-date id="date4" value="2014/12/25 11:45:30"></x-date>
+ <x-date id="date5" value="2014/12/25 11:45:30:33"></x-date>
<script>
var assert = chai.assert;
@@ -117,11 +115,9 @@
// Date deserialization tests
assert.equal(String(document.querySelector("#date1").value), String(new Date(2014, 11, 25)));
assert.equal(String(document.querySelector("#date2").value), String(new Date(2014, 11, 25)));
- assert.equal(String(document.querySelector("#date3").value), String(new Date(2014, 11, 25)));
- assert.equal(String(document.querySelector("#date4").value), String(new Date(2014, 11, 25)));
- assert.equal(String(document.querySelector("#date5").value), String(new Date(2014, 11, 25, 11, 45)));
- assert.equal(String(document.querySelector("#date6").value), String(new Date(2014, 11, 25, 11, 45, 30)));
- assert.equal(document.querySelector("#date7").value.getMilliseconds(), new Date(2014, 11, 25, 11, 45, 30, 33).getMilliseconds());
+ assert.equal(String(document.querySelector("#date3").value), String(new Date(2014, 11, 25, 11, 45)));
+ assert.equal(String(document.querySelector("#date4").value), String(new Date(2014, 11, 25, 11, 45, 30)));
+ assert.equal(document.querySelector("#date5").value.getMilliseconds(), new Date(2014, 11, 25, 11, 45, 30, 33).getMilliseconds());
//
done();
});
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

0 comments on commit ec15352

Please sign in to comment.