Skip to content
Merged
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
35 changes: 21 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const METHOD = 'map-get';

const valResolve = value => {
const map = value.substring(
value.indexOf('((') + 2,
value.indexOf('),')
)
value.indexOf('((') + 2,
value.indexOf('),')
)
.split(',')
.reduce((map, string) => {
const [key, value] = string.split(':');
return Object.assign(map, {[key]: value})
return Object.assign(map, {[key]: value});
}, {});

const key = value.substring(
Expand All @@ -19,18 +19,25 @@ const valResolve = value => {
);

return map[key];
}
};

const normalize = value => value.replace(/(\s|!default)/g, '');

export default postcss.plugin('postcss-map-get', () => {
return nodes => {
nodes.walkDecls(decl => {
let {value} = decl;

if (value.startsWith(METHOD)) {
decl.value = valResolve(normalize(decl.value));
}
});
};
return nodes => {
nodes.walkDecls(decl => {
let {value} = decl;

if (value.startsWith(METHOD)) {
decl.value = valResolve(normalize(decl.value));
}
});

nodes.walkAtRules(rules => {
const {params} = rules;
const start = params.indexOf(METHOD);
const end = params.lastIndexOf('))');
rules.params = `${params.substr(0, start)}${valResolve(normalize(params.substring(start, end)))}${params.substr(end + 1)}`;
});
};
});
20 changes: 13 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ import test from 'ava';
import plugin from '../src';

const processing = (input, opts) => {
return postcss([plugin(opts)]).process(input).css;
return postcss([plugin(opts)]).process(input).css;
};

test('it should return body color for background', t => {
const expected = 'body{background: #fff;}';
const value = 'body{background: map-get((body: #fff,main-red: #c53831,link-blue: #0592fb) !default, body);}';
t.is(processing(value), expected);
const expected = 'body{background: #fff;}';
const value = 'body{background: map-get((body: #fff,main-red: #c53831,link-blue: #0592fb) !default, body);}';
t.is(processing(value), expected);
});

test('it should return body color and min-width width fro decl', t => {
const expected = 'body{background: #fff;min-width: 1280px;}';
const value = 'body{background: map-get((body: #fff,main-red: #c53831,link-blue: #0592fb) !default, body);min-width: map-get((xxs: 0,xs: 576px,sm: 768px,md: 992px,lg: 1280px,xl: 1360px,xxl: 1600px) !default, lg);}';
t.is(processing(value), expected);
const expected = 'body{background: #fff;min-width: 1280px;}';
const value = 'body{background: map-get((body: #fff,main-red: #c53831,link-blue: #0592fb) !default, body);min-width: map-get((xxs: 0,xs: 576px,sm: 768px,md: 992px,lg: 1280px,xl: 1360px,xxl: 1600px) !default, lg);}';
t.is(processing(value), expected);
});

test('it should retunr width for at rule @media', t => {
const expected = '@media (min-width: 1280px) {body {overflow-x: hidden}}';
const value = '@media (min-width: map-get((xxs: 0,xs: 576px,sm: 768px,md: 992px,lg: 1280px,xl: 1360px,xxl: 1600px) !default, lg))) {body {overflow-x: hidden}}';
t.is(processing(value), expected);
});