Skip to content

Commit

Permalink
Merge pull request #122 from bsatrom/date-attrs
Browse files Browse the repository at this point in the history
added deserialization of Date attributes for custom elements
  • Loading branch information
Scott J. Miles committed May 8, 2013
2 parents 8d8ab65 + ec15352 commit 55a7a2f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion platform
11 changes: 8 additions & 3 deletions src/attrs.js
Expand Up @@ -62,7 +62,7 @@
function takeAttributes() {
// for each attribute
forEach(this.attributes, function(a) {
// try to match this attribute to a property (attributess are
// try to match this attribute to a property (attributes are
// all lower-case, so this is case-insensitive search)
var name = propertyForAttribute.call(this, a.name);
if (name) {
Expand All @@ -87,15 +87,15 @@

var lowerCase = String.prototype.toLowerCase.call.bind(
String.prototype.toLowerCase);

// return the published property matching name, or undefined
function propertyForAttribute(name) {
// matchable properties must be published
var properties = Object.keys(this[published$]);
// search for a matchable property
return properties[properties.map(lowerCase).indexOf(name.toLowerCase())];
};

function deserializeValue(inValue, inDefaultValue) {
var inferredType = typeof inDefaultValue;
if (inferredType === 'string') {
Expand All @@ -109,6 +109,11 @@
case 'false':
return false;
}

if (inDefaultValue instanceof Date) {
return new Date(Date.parse(inValue) || Date.now());
}

var float = parseFloat(inValue);
return (String(float) === inValue) ? float : inValue;
}
Expand Down
21 changes: 21 additions & 0 deletions test/html/take-attributes.html
Expand Up @@ -59,6 +59,20 @@
<x-zot id="zot4" bool="squid"></x-zot>
<x-zot id="zot5" bool="0"></x-zot>

<element name="x-date" attributes="value">
<script>
Toolkit.register(this, {
value: new Date()
});
</script>
</element>

<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;
document.addEventListener('WebComponentsReady', function() {
Expand Down Expand Up @@ -97,6 +111,13 @@
//
assert.equal(document.querySelector("#zot0").num, 84);
assert.equal(document.querySelector("#zot0").str, "don't panic");
//
// 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, 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();
});
Expand Down
9 changes: 5 additions & 4 deletions toolkit.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion toolkit.min.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions toolkit.native.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion toolkit.native.min.js.map

Large diffs are not rendered by default.

0 comments on commit 55a7a2f

Please sign in to comment.