Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registerFilter method #233

Merged
merged 7 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/filterProperties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('filterProperties', () => {
expect(filteredDictionary.properties).not.toHaveProperty('color');
});

it('should work with a filter object', () => {
xit('should work with a filter object', () => {
didoo marked this conversation as resolved.
Show resolved Hide resolved
var filter = { "attributes": { "category": "size" } };
var filteredDictionary = filterProperties(dictionary, filter);
_.each(filteredDictionary.allProperties, function(property) {
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ var StyleDictionary = {
transformGroup: require('./lib/common/transformGroups'),
format: require('./lib/common/formats'),
action: require('./lib/common/actions'),
filter: {}, // we need to initialise the object, since we don't have built-in filters

registerTransform: require('./lib/register/transform'),
registerTransformGroup: require('./lib/register/transformGroup'),
registerFormat: require('./lib/register/format'),
registerTemplate: require('./lib/register/template'),
registerAction: require('./lib/register/action'),
registerFilter: require('./lib/register/filter'),

exportPlatform: require('./lib/exportPlatform'),
buildPlatform: require('./lib/buildPlatform'),
Expand Down
12 changes: 5 additions & 7 deletions lib/filterProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ function filterPropertyArray(properties, filter) {
* object using a function provided by the user.
*
* @param {Object} dictionary
* @param {Function|Object} filter - Either a function that receives a property
* object and returns `true` if the property should be included in the output
* or `false` if the property should be excluded from the output, or an object
* to compare each property to (see lodash/matches).
* @param {Function} filter - A function that receives a property object
* and returns `true` if the property should be included in the output
* or `false` if the property should be excluded from the output
* @returns {Object} dictionary - A new dictionary containing only the
* properties that matched the filter (or the original dictionary if no filter
* function was provided).
Expand All @@ -82,10 +81,9 @@ function filterProperties(dictionary, filter) {
if (!filter) {
return dictionary
} else {
const filterFunc = _.isFunction(filter) ? filter : _.matches(filter)
return _.assign({}, dictionary, {
allProperties: filterPropertyArray(dictionary.allProperties, filterFunc),
properties: filterPropertyObject(dictionary.properties, filterFunc)
allProperties: filterPropertyArray(dictionary.allProperties, filter),
properties: filterPropertyObject(dictionary.properties, filter)
})
}
}
Expand Down
43 changes: 43 additions & 0 deletions lib/register/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

/**
* Add a custom format to the style dictionary
* @static
* @memberof module:style-dictionary
* @param {Object} filter
* @param {String} filter.name - Name of the filter to be referenced in your config.json
* @param {Function} filter.matcher - Matcher function, return boolean if the property should be included.
* @returns {module:style-dictionary}
* @example
* ```js
* StyleDictionary.registerFilter({
* name: 'isColor',
* matcher: function(prop) {
* return prop.attributes.category === 'color';
* }
* })
* ```
*/
function registerFilter(options) {
if (typeof options.name !== 'string')
throw new Error('Can\'t register filter; filter.name must be a string');
if (typeof options.matcher !== 'function')
throw new Error('Can\'t register filter; filter.matcher must be a function');

this.filter[options.name] = options.matcher;

return this;
}

module.exports = registerFilter;
29 changes: 23 additions & 6 deletions lib/transform/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
*/

var _ = require('lodash'),
chalk = require('chalk'),
GroupMessages = require('../utils/groupMessages');

var TEMPLATE_DEPRECATION_WARNINGS = GroupMessages.GROUP.TemplateDeprecationWarnings;

/**
* Takes a platform config object and returns a new one
* that has transforms, formats, and actions
* that has filters, transforms, formats, and actions
* mapped properly.
* @private
* @param {Object} config
Expand All @@ -38,8 +37,9 @@ function transformConfig(config, dictionary) {
if (to_ret.transforms) {
transforms = to_ret.transforms;
} else if (to_ret.transformGroup) {
transforms = dictionary.transformGroup[to_ret.transformGroup];
if (!transforms) {
if (dictionary.transformGroup[to_ret.transformGroup]) {
transforms = dictionary.transformGroup[to_ret.transformGroup];
} else {
throw new Error('transformGroup ' + to_ret.transformGroup + ' doesn\'t exist');
}
}
Expand All @@ -52,25 +52,42 @@ function transformConfig(config, dictionary) {
});

to_ret.files = _.map(config.files, function(file) {
const ext = {};
if (file.filter) {
if(typeof file.filter === 'string') {
if (dictionary.filter[file.filter]) {
ext.filter = dictionary.filter[file.filter];
} else {
throw new Error('Can\'t find filter: ' + file.filter);
}
} else if (typeof file.filter === 'object') {
ext.filter = _.matches(file.filter);
} else if (typeof file.filter === 'function') {
ext.filter = file.filter;
} else {
throw new Error('Filter format not valid: ' + typeof file.filter);
}
}
if (file.template) {
if (dictionary.format[file.template]) {
GroupMessages.add(
TEMPLATE_DEPRECATION_WARNINGS,
`${file.destination} (template: ${file.template})`
);
return _.extend({}, file, {format: dictionary.format[file.template]});
ext.format = dictionary.format[file.template];
} else {
throw new Error('Can\'t find template: ' + file.template);
}
} else if (file.format) {
if (dictionary.format[file.format]) {
return _.extend({}, file, {format: dictionary.format[file.format]});
ext.format = dictionary.format[file.format];
} else {
throw new Error('Can\'t find format: ' + file.format);
}
} else {
throw new Error('Please supply a format for file: ' + JSON.stringify(file));
}
return _.extend({}, file, ext);
});

to_ret.actions = _.map(config.actions, function(action) {
Expand Down