Skip to content

Commit

Permalink
Fixes #56 (#61)
Browse files Browse the repository at this point in the history
* Fixes #56
* Ability to have non-string values that retain their type
* Allow non-property leaf nodes (that don't have a value attribute) in a style dictionary
  • Loading branch information
dbanksdesign authored Feb 28, 2018
1 parent 6eb3b9d commit ab5f0bf
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/transform/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ function transformObject(obj, options, path, to_ret) {
if (obj.hasOwnProperty(name)) {
path.push(name);
// Need better logic
if (_.isPlainObject(obj[name]) && !_.has(obj[name], 'value')) {
to_ret[name] = transformObject(obj[name], options, path, to_ret[name]);
} else {
if (_.isPlainObject(obj[name]) && _.has(obj[name], 'value')) {
to_ret[name] = transformProperty(
propertySetup(obj[name], name, path),
options
);
} else if (_.isPlainObject(obj[name])) {
to_ret[name] = transformObject(obj[name], options, path, to_ret[name]);
} else {
to_ret[name] = obj[name];
}
path.pop();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/transform/property.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function transformProperty(property, options) {
if (transform.type === 'name')
to_ret.name = transform.transformer(to_ret, options);
// Don't try to transform the value if it is referencing another value
if (transform.type === 'value' && property.value.indexOf('{') < 0)
if (transform.type === 'value' && _.isString(property.value) && property.value.indexOf('{') < 0)
to_ret.value = transform.transformer(to_ret, options);
if (transform.type === 'attribute')
to_ret.attributes = _.extend({}, to_ret.attributes, transform.transformer(to_ret, options));
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/flattenProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ function flattenProperties(properties, to_ret) {
for(var name in properties) {
if (properties.hasOwnProperty(name)) {
// TODO: this is a bit fragile and arbitrary to stop when we get to a 'value' property.
if (_.isPlainObject(properties[name]) && !_.has(properties[name], 'value')) {
flattenProperties(properties[name], to_ret);
} else {
if (_.isPlainObject(properties[name]) && _.has(properties[name], 'value')) {
to_ret.push( properties[name] );
} else if (_.isPlainObject(properties[name])) {
flattenProperties(properties[name], to_ret);
}
}
}
Expand Down
Binary file removed test/assets/fonts/scapp_icons-regular.ttf
Binary file not shown.
50 changes: 50 additions & 0 deletions test/buildPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,54 @@ describe('buildPlatform', function() {
assert(helpers.fileExists('./test/output/ios/style_dictionary.plist'));
assert(helpers.fileExists('./test/output/ios/style_dictionary.h'));
});

it('should handle non-string values in properties', function() {
var test = StyleDictionary.extend({
source: ['test/properties/nonString.json'],
platforms: {
test: {
buildPath: "test/output/",
transformGroup: "scss",
files: [
{
"destination": "output.json",
"format": "json"
}
]
}
}
});
test.buildPlatform('test');
assert(helpers.fileExists('./test/output/output.json'));
var input = helpers.fileToJSON('./test/properties/nonString.json');
var output = helpers.fileToJSON('./test/output/output.json');
assert.deepEqual(output.color.otherRed.value, input.color.red.value);
assert.deepEqual(output.color.otherBlue.value, input.color.blue.value);
assert.equal(output.size.otherLarge.value, input.size.large.value);
});

it('should handle non-property nodes', function() {
var test = StyleDictionary.extend({
source: ['test/properties/nonPropertyNode.json'],
platforms: {
test: {
buildPath: "test/output/",
transformGroup: "scss",
files: [
{
"destination": "output.json",
"format": "json"
}
]
}
}
});
test.buildPlatform('test');
assert(helpers.fileExists('./test/output/output.json'));
var input = helpers.fileToJSON('./test/properties/nonPropertyNode.json');
var output = helpers.fileToJSON('./test/output/output.json');
assert.deepEqual(output.color.comment, input.color.comment);
assert.deepEqual(output.color.base.comment, input.color.base.comment);
assert.equal(output.color.base.attributes.comment, input.color.base.attributes.comment);
});
});
12 changes: 12 additions & 0 deletions test/properties/nonPropertyNode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"color": {
"base": {
"red": { "value": "#f00" },
"comment": "testing",
"attributes": {
"comment": "more testing"
}
},
"comment": "testing"
}
}
30 changes: 30 additions & 0 deletions test/properties/nonString.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"color": {
"red": {
"value": {
"r": 255,
"g": 0,
"b": 0,
"a": 1
}
},
"blue": {
"value": [0,0,255,1]
},
"otherRed": {
"value": "{color.red.value}"
},
"otherBlue": {
"value": "{color.blue.value}"
}
},

"size": {
"large": {
"value": 20
},
"otherLarge": {
"value": "{size.large.value}"
}
}
}

0 comments on commit ab5f0bf

Please sign in to comment.