Skip to content

Handle Sass values #4

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const csscolors = require('css-color-names');

const exists = (file) => {
try {
Expand All @@ -10,14 +11,33 @@ const exists = (file) => {
}
}

const reValue = new RegExp([
// ---- hex colors
'^#[0-9a-z]{3}$', // #def
'^#[0-9a-z]{4}$', // #def0
'^#[0-9a-z]{6}$', // #ddeeff
'^#[0-9a-z]{8}$', // #ddeeff00
// ---- numbers
'^[0-9]+(?:\\.[0-9]+)?(?:%|[a-z]+)?$', // 12.34 + unit
// ---- function calls
'^[a-z][a-z0-9-_]*\\(', // foo(...)
].join('|'), 'i');

const looksLikeString = (value) => {
return !reValue.test(value) || csscolors[value];
}

const buildSassValue = (value) => {
if (Array.isArray(value)) {
return `(${value.reduce((prev, cur) => prev + `"${cur}",`, '')})`;
return `(${value.reduce((prev, cur) => prev + `${buildSassValue(cur)},`, '')})`;
}
if (typeof value === "object") {
return `(${buildSassMap(value)})`;
}
return `"${value}"`;
if (looksLikeString(value)) {
return `"${value}"`;
}
return value;
}

const buildSassMap = (json) => {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
"devDependencies": {
"jest": "^23.4.0",
"node-sass": "^4.9.2"
},
"dependencies": {
"css-color-names": "0.0.4"
}
}
16 changes: 14 additions & 2 deletions tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`json-importer async should not resolve Sass values to quoted strings 1`] = `
"output {
contents: (\\"short-hex-color\\": #def, \\"short-hex-color-with-alpha\\": #def0, \\"long-hex-color\\": #ddeeff, \\"long-hex-color-with-alpha\\": #ddeeff00, \\"named-color\\": \\"red\\", \\"number\\": 16, \\"number-with-percentage\\": 16%, \\"number-with-word-unit\\": 16rem, \\"decimal\\": 12.34, \\"decimal-with-percentage\\": 12.34%, \\"decimal-with-word-unit\\": 12.34rem, \\"function-call\\": black, \\"string\\": \\"foo\\", \\"string-with-spaces\\": \\"who are you\\", \\"array\\": (16, 16px, 16%, 12.34, 12.34px, 12.34%, #def, black, \\"foo\\", \\"who are you\\"), \\"map\\": (\\"short-hex-color\\": #def, \\"short-hex-color-with-alpha\\": #def0, \\"string\\": \\"foo\\", \\"string-with-spaces\\": \\"who are you\\")); }
"
`;

exports[`json-importer async should resolve flat json strings as Sass map 1`] = `
"output {
contents: (\\"color\\": \\"red\\", \\"size\\": \\"small\\"); }
Expand All @@ -20,7 +26,13 @@ exports[`json-importer async should resolve json files in includePaths 1`] = `

exports[`json-importer async should resolve nested json strings as Sass map 1`] = `
"output {
contents: (\\"colors\\": (\\"red\\": \\"#f00\\", \\"blue\\": \\"#00f\\"), \\"sizes\\": (\\"small\\": \\"16px\\", \\"big\\": \\"30px\\")); }
contents: (\\"colors\\": (\\"red\\": #f00, \\"blue\\": #00f), \\"sizes\\": (\\"small\\": 16px, \\"big\\": 30px)); }
"
`;

exports[`json-importer sync should not resolve Sass values to quoted strings 1`] = `
"output {
contents: (\\"short-hex-color\\": #def, \\"short-hex-color-with-alpha\\": #def0, \\"long-hex-color\\": #ddeeff, \\"long-hex-color-with-alpha\\": #ddeeff00, \\"named-color\\": \\"red\\", \\"number\\": 16, \\"number-with-percentage\\": 16%, \\"number-with-word-unit\\": 16rem, \\"decimal\\": 12.34, \\"decimal-with-percentage\\": 12.34%, \\"decimal-with-word-unit\\": 12.34rem, \\"function-call\\": black, \\"string\\": \\"foo\\", \\"string-with-spaces\\": \\"who are you\\", \\"array\\": (16, 16px, 16%, 12.34, 12.34px, 12.34%, #def, black, \\"foo\\", \\"who are you\\"), \\"map\\": (\\"short-hex-color\\": #def, \\"short-hex-color-with-alpha\\": #def0, \\"string\\": \\"foo\\", \\"string-with-spaces\\": \\"who are you\\")); }
"
`;

Expand All @@ -44,6 +56,6 @@ exports[`json-importer sync should resolve json files in includePaths 1`] = `

exports[`json-importer sync should resolve nested json strings as Sass map 1`] = `
"output {
contents: (\\"colors\\": (\\"red\\": \\"#f00\\", \\"blue\\": \\"#00f\\"), \\"sizes\\": (\\"small\\": \\"16px\\", \\"big\\": \\"30px\\")); }
contents: (\\"colors\\": (\\"red\\": #f00, \\"blue\\": #00f), \\"sizes\\": (\\"small\\": 16px, \\"big\\": 30px)); }
"
`;
34 changes: 34 additions & 0 deletions tests/fixtures/values.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"short-hex-color": "#def",
"short-hex-color-with-alpha": "#def0",
"long-hex-color": "#ddeeff",
"long-hex-color-with-alpha": "#ddeeff00",
"named-color": "red",
"number": "16",
"number-with-percentage": "16%",
"number-with-word-unit": "16rem",
"decimal": "12.34",
"decimal-with-percentage": "12.34%",
"decimal-with-word-unit": "12.34rem",
"function-call": "hsl(0,0,0)",
"string": "foo",
"string-with-spaces": "who are you",
"array": [
16,
"16px",
"16%",
12.34,
"12.34px",
"12.34%",
"#def",
"hsl(0,0,0)",
"foo",
"who are you"
],
"map": {
"short-hex-color": "#def",
"short-hex-color-with-alpha": "#def0",
"string": "foo",
"string-with-spaces": "who are you"
}
}
4 changes: 4 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ describe('json-importer', () => {
func(generate('tests/fixtures/list.json'))
.then(result => expect(result).toMatchSnapshot())
));
it('should not resolve Sass values to quoted strings', () => (
func(generate('tests/fixtures/values.json'))
.then(result => expect(result).toMatchSnapshot())
));
it('should resolve json files in includePaths', () => (
func(generate('fixtures/flat.json'), { includePaths: ['tests']})
.then(result => expect(result).toMatchSnapshot())
Expand Down