Skip to content

Commit

Permalink
change filterByName to filter method
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann committed Oct 3, 2020
1 parent 5a5ef45 commit f8f5a9e
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 57 deletions.
54 changes: 25 additions & 29 deletions dist/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,9 @@
};
exports.convertRgbaObjectToString = (rgbaObject) => `rgba(${rgbaObject.r}, ${rgbaObject.g}, ${rgbaObject.b}, ${rgbaObject.a})`;
});
define("src/utilities/filterByNameProperty", ["require", "exports"], function (require, exports) {
define("src/extractor/extractColors", ["require", "exports", "src/utilities/convertColor", "src/utilities/roundWithDecimals"], function (require, exports, convertColor_1, roundWithDecimals_2) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const excludeUnderscoreStyles = true;
const filterByNameProperty = (objects) => {
if (excludeUnderscoreStyles === true) {
return objects.filter(style => style.name.trim().substr(0, 1) !== '_');
}
return objects;
};
exports.default = filterByNameProperty;
});
define("src/extractor/extractColors", ["require", "exports", "src/utilities/convertColor", "src/utilities/filterByNameProperty", "src/utilities/roundWithDecimals"], function (require, exports, convertColor_1, filterByNameProperty_1, roundWithDecimals_2) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
filterByNameProperty_1 = __importDefault(filterByNameProperty_1);
roundWithDecimals_2 = __importDefault(roundWithDecimals_2);
const gradientType = {
"GRADIENT_LINEAR": "linear",
Expand Down Expand Up @@ -119,7 +106,7 @@
};
const extractColors = (tokenNodes) => {
// get all paint styles
return filterByNameProperty_1.default(tokenNodes)
return tokenNodes
// remove images fills from tokens
.map(node => {
node.paints = node.paints.filter(paint => paint.type !== "IMAGE");
Expand All @@ -138,10 +125,9 @@
};
exports.default = extractColors;
});
define("src/extractor/extractGrids", ["require", "exports", "src/utilities/filterByNameProperty"], function (require, exports, filterByNameProperty_2) {
define("src/extractor/extractGrids", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
filterByNameProperty_2 = __importDefault(filterByNameProperty_2);
const gridValues = (grid) => ({
pattern: {
value: grid.pattern.toLowerCase()
Expand Down Expand Up @@ -185,7 +171,7 @@
} })));
const extractGrids = (tokenNodes) => {
// get grid styles
return filterByNameProperty_2.default(tokenNodes).map(node => ({
return tokenNodes.map(node => ({
name: node.name,
description: node.description || null,
category: 'grid',
Expand All @@ -194,10 +180,9 @@
};
exports.default = extractGrids;
});
define("src/extractor/extractFonts", ["require", "exports", "src/utilities/filterByNameProperty", "src/utilities/roundWithDecimals"], function (require, exports, filterByNameProperty_3, roundWithDecimals_3) {
define("src/extractor/extractFonts", ["require", "exports", "src/utilities/roundWithDecimals"], function (require, exports, roundWithDecimals_3) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
filterByNameProperty_3 = __importDefault(filterByNameProperty_3);
roundWithDecimals_3 = __importDefault(roundWithDecimals_3);
const textDecorations = {
'NONE': 'none',
Expand All @@ -212,7 +197,7 @@
};
const extractFonts = (tokenNodes) => {
// get raw text styles
return filterByNameProperty_3.default(tokenNodes).map(node => ({
return tokenNodes.map(node => ({
name: node.name,
description: node.description || undefined,
values: {
Expand Down Expand Up @@ -263,10 +248,9 @@
};
exports.default = extractFonts;
});
define("src/extractor/extractEffects", ["require", "exports", "src/utilities/convertColor", "src/utilities/filterByNameProperty"], function (require, exports, convertColor_2, filterByNameProperty_4) {
define("src/extractor/extractEffects", ["require", "exports", "src/utilities/convertColor"], function (require, exports, convertColor_2) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
filterByNameProperty_4 = __importDefault(filterByNameProperty_4);
const effectType = {
"LAYER_BLUR": 'layerBlur',
"BACKGROUND_BLUR": 'backgroundBlur',
Expand Down Expand Up @@ -318,7 +302,7 @@
});
const extractEffects = (tokenNodes) => {
// get effect styles
return filterByNameProperty_4.default(tokenNodes).map(node => ({
return tokenNodes.map(node => ({
name: node.name,
description: node.description || null,
category: 'effect',
Expand Down Expand Up @@ -675,6 +659,14 @@
};
exports.default = tokenExport;
});
define("src/utilities/filterByNameProperty", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const filterByPropertyName = (prefix = '_', exclude = true) => {
return (object) => (object.name.trim().substr(0, prefix.length) !== prefix) === exclude;
};
exports.default = filterByPropertyName;
});
define("src/utilities/getTokenFrames", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Expand Down Expand Up @@ -713,20 +705,24 @@
};
exports.default = getTokenFrames;
});
define("src/utilities/buildFigmaData", ["require", "exports", "src/utilities/getTokenFrames"], function (require, exports, getTokenFrames_1) {
define("src/utilities/buildFigmaData", ["require", "exports", "src/utilities/filterByNameProperty", "src/utilities/getTokenFrames"], function (require, exports, filterByNameProperty_1, getTokenFrames_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
filterByNameProperty_1 = __importDefault(filterByNameProperty_1);
getTokenFrames_1 = __importDefault(getTokenFrames_1);
const buildFigmaData = (figma) => {
// use spread operator because the original is readOnly
const tokenFrames = getTokenFrames_1.default([...figma.root.children]);
// filter options
const filterPrefix = '_';
const filterExclude = true;
// get data from figma
return {
tokenFrames: tokenFrames,
paintStyles: figma.getLocalPaintStyles(),
gridStyles: figma.getLocalGridStyles(),
textStyles: figma.getLocalTextStyles(),
effectStyles: figma.getLocalEffectStyles()
paintStyles: figma.getLocalPaintStyles().filter(filterByNameProperty_1.default(filterPrefix, filterExclude)),
gridStyles: figma.getLocalGridStyles().filter(filterByNameProperty_1.default(filterPrefix, filterExclude)),
textStyles: figma.getLocalTextStyles().filter(filterByNameProperty_1.default(filterPrefix, filterExclude)),
effectStyles: figma.getLocalEffectStyles().filter(filterByNameProperty_1.default(filterPrefix, filterExclude)),
};
};
exports.default = buildFigmaData;
Expand Down
3 changes: 1 addition & 2 deletions src/extractor/extractColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import extractorInterface from '../../types/extractorInterface'
import { colorPropertyInterface, fillValuesType, gradientValuesType } from '../../types/propertyObject'
import { GradientType, PropertyType } from '../../types/valueTypes'
import { convertPaintToRgba, roundRgba } from '../utilities/convertColor'
import filterByNameProperty from '../utilities/filterByNameProperty'
import roundWithDecimals from '../utilities/roundWithDecimals'

const gradientType = {
Expand Down Expand Up @@ -48,7 +47,7 @@ const extractFills = (paint): fillValuesType | gradientValuesType => {

const extractColors: extractorInterface = (tokenNodes: PaintStyle[]): colorPropertyInterface[] => {
// get all paint styles
return filterByNameProperty(tokenNodes)
return tokenNodes
// remove images fills from tokens
.map(node => {
node.paints = node.paints.filter(paint => paint.type !== "IMAGE")
Expand Down
3 changes: 1 addition & 2 deletions src/extractor/extractEffects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import extractorInterface from '../../types/extractorInterface'
import { effectPropertyInterface } from '../../types/propertyObject'
import { EffectType, UnitTypePixel, PropertyType } from '../../types/valueTypes'
import { roundRgba } from '../utilities/convertColor'
import filterByNameProperty from '../utilities/filterByNameProperty'

const effectType = {
"LAYER_BLUR": 'layerBlur',
Expand Down Expand Up @@ -58,7 +57,7 @@ const shadowValues = effect => ({

const extractEffects: extractorInterface = (tokenNodes: EffectStyle[]): effectPropertyInterface[] => {
// get effect styles
return filterByNameProperty(tokenNodes).map(node => ({
return tokenNodes.map(node => ({
name: node.name,
description: node.description || null,
category: 'effect',
Expand Down
3 changes: 1 addition & 2 deletions src/extractor/extractFonts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import extractorInterface from '../../types/extractorInterface'
import { fontPropertyInterface } from '../../types/propertyObject'
import { UnitTypePixel, TextCase, TextDecoration, NumericUnitTypes, PropertyType } from '../../types/valueTypes'
import filterByNameProperty from '../utilities/filterByNameProperty'
import roundWithDecimals from '../utilities/roundWithDecimals'

const textDecorations = {
Expand All @@ -19,7 +18,7 @@ const textCases = {

const extractFonts: extractorInterface = (tokenNodes: TextStyle[]): fontPropertyInterface[] => {
// get raw text styles
return filterByNameProperty(tokenNodes).map(node => ({
return tokenNodes.map(node => ({
name: node.name,
description: node.description || undefined,
values: {
Expand Down
3 changes: 1 addition & 2 deletions src/extractor/extractGrids.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import extractorInterface from '../../types/extractorInterface'
import { gridPropertyInterface } from '../../types/propertyObject'
import { GridAlignment, GridPattern, PropertyType } from '../../types/valueTypes'
import filterByNameProperty from '../utilities/filterByNameProperty'

const gridValues = (grid: GridLayoutGrid) => ({
pattern: {
Expand Down Expand Up @@ -58,7 +57,7 @@ const rowColumnValues = (grid: RowsColsLayoutGrid) => ({

const extractGrids: extractorInterface = (tokenNodes: GridStyle[]): gridPropertyInterface[] => {
// get grid styles
return filterByNameProperty(tokenNodes).map(node => ({
return tokenNodes.map(node => ({
name: node.name,
description: node.description || null,
category: 'grid',
Expand Down
12 changes: 8 additions & 4 deletions src/utilities/buildFigmaData.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { figmaDataType } from '../../types/figmaDataType'
import filterByNameProperty from './filterByNameProperty'
import getTokenFrames from './getTokenFrames'

const buildFigmaData = (figma: PluginAPI): figmaDataType => {
// use spread operator because the original is readOnly
const tokenFrames = getTokenFrames([...figma.root.children])
// filter options
const filterPrefix = '_'
const filterExclude = true
// get data from figma
return {
tokenFrames: tokenFrames,
paintStyles: figma.getLocalPaintStyles(),
gridStyles: figma.getLocalGridStyles(),
textStyles: figma.getLocalTextStyles(),
effectStyles: figma.getLocalEffectStyles()
paintStyles: figma.getLocalPaintStyles().filter(filterByNameProperty(filterPrefix, filterExclude)),
gridStyles: figma.getLocalGridStyles().filter(filterByNameProperty(filterPrefix, filterExclude)),
textStyles: figma.getLocalTextStyles().filter(filterByNameProperty(filterPrefix, filterExclude)),
effectStyles: figma.getLocalEffectStyles().filter(filterByNameProperty(filterPrefix, filterExclude)),
}
}

Expand Down
11 changes: 3 additions & 8 deletions src/utilities/filterByNameProperty.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
const excludeUnderscoreStyles = true

type objectWithNameProperty = {
name: string,
[key: string]: any
}

const filterByNameProperty = (objects: objectWithNameProperty[]): any[] => {
if (excludeUnderscoreStyles === true) {
return objects.filter(style => style.name.trim().substr(0, 1) !== '_')
}
return objects
const filterByPropertyName = (prefix: string = '_', exclude: boolean = true) => {
return (object: objectWithNameProperty) => (object.name.trim().substr(0, prefix.length) !== prefix) === exclude
}

export default filterByNameProperty
export default filterByPropertyName
2 changes: 1 addition & 1 deletion tests/files/design-tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"sizes": {
"32": {
"category": "size",
"comment": "32px spacer component",
"comment": "32.72px spacer component",
"value": 32.72,
"type": "number",
"unit": "pixel"
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/data/cssOutput.data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default
`:root {
--sizes-32: 32.72; /* 32px spacer component */
--sizes-32: 32.72; /* 32.72px spacer component */
--sizes-frame: 32;
--sizes-rect: 32;
--sizes-shape-in-component: 32; /* Should use 32px not 20 from inside shape */
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/data/variables.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* Do not edit directly
* Generated on Fri, 02 Oct 2020 14:06:02 GMT
* Generated on Fri, 02 Oct 2020 21:40:41 GMT
*/

:root {
--sizes-32: 32.72; /* 32px spacer component */
--sizes-32: 32.72; /* 32.72px spacer component */
--sizes-frame: 32;
--sizes-rect: 32;
--sizes-shape-in-component: 32; /* Should use 32px not 20 from inside shape */
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/filterByNameProperty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import filterByNameProperty from '../../src/utilities/filterByNameProperty'

describe("getTokenStyles", () => {
test("exclude _ prefix", () => {
expect(filterByNameProperty([
expect([
{
id: "valid",
type: "PAINT",
Expand All @@ -15,7 +15,7 @@ describe("getTokenStyles", () => {
name: '_invalid',
description: ''
}
])).toStrictEqual([
].filter(filterByNameProperty())).toStrictEqual([
{
id: "valid",
type: "PAINT",
Expand Down
4 changes: 2 additions & 2 deletions types/propertyObject.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type fontPropertyInterface = propertyObject & {
letterSpacing: numericPropertyType,
lineHeight: {
value: number | "normal"
unit: "pixel" | "percent" | "auto"
unit: string // "pixel" | "percent" | "auto"
},
paragraphIndent: numericPropertyType,
paragraphSpacing: numericPropertyType,
Expand Down Expand Up @@ -71,7 +71,7 @@ export type gradientValuesType = {
}

export type colorPropertyInterface = propertyObject & {
values: fillValuesType | gradientValuesType
values: (fillValuesType | gradientValuesType)[]
}

export type sizePropertyInterface = propertyObject & {
Expand Down

0 comments on commit f8f5a9e

Please sign in to comment.