Last active
August 29, 2015 14:09
-
-
Save cahnory/1ae30579185363e6fc07 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// Sass (v3.4.7) | |
// Compass (v1.0.1) | |
// ---- | |
@function map-set($map, $key, $value) { | |
@return map-merge($map, ($key: $value)); | |
} | |
$DATA__VALUES: ( | |
type: root, | |
values: () | |
); | |
// public functions | |
@function data-set($path, $value: null, $data: $DATA__VALUES) { | |
@if 'map' == type-of($path) { | |
$value: $path; | |
@each $key, $val in $value { | |
$data: data-set($key, $val, $data); | |
} | |
} | |
@else { | |
$path: data__parse-path($path); | |
@if 2 == length($path) { | |
// update child node | |
$child: map-get(map-get($data, 'values'), nth($path, 1)); | |
@if not $child or 'map' != map-get($child, 'type') { | |
$child: (type: 'map', values: ()); | |
} | |
$child: data-set(nth($path, 2), $value, $child); | |
// update node | |
$data: map-set($data, 'values', map-set(map-get($data, 'values'), nth($path, 1), $child)); | |
} | |
@else if '' != $path { | |
$data: map-set($data, 'values', map-set(map-get($data, 'values'), $path, data__parse-value($value))); | |
} | |
} | |
// save global data | |
@if 'root' == map-get($data, 'type') { | |
$DATA__VALUES: $data!global; | |
} | |
@return $data; | |
} | |
@function data-get($path: '', $data: $DATA__VALUES) { | |
$path: data__parse-path($path); | |
$value: null; | |
@if 2 == length($path) { | |
$values: map-get($data, 'values'); | |
@if map-has-key($values, nth($path, 1)) { | |
$value: data-get(nth($path, 2), map-get($values, nth($path, 1))); | |
} | |
} | |
@else if '' != $path { | |
$value: data__render-value(map-get(map-get($data, 'values'), $path)); | |
} | |
@else { | |
$value: data__render-value($data); | |
} | |
@return $value; | |
} | |
@function data($args...) { | |
@if length($args) > 1 or 'map' == type-of(nth($args, 1)) { | |
@return data-set($args...); | |
} | |
@return data-get($args...); | |
} | |
@mixin data($args...) { | |
@if data($args...){} | |
} | |
// private functions | |
@function data__parse-path($path) { | |
$path: '#{$path}'; | |
$index: str-index($path, '.'); | |
@if $index { | |
@return str-slice($path, 1, $index - 1), str-slice($path, $index + 1); | |
} | |
@return $path; | |
} | |
@function data__parse-value($value) { | |
$type: type-of($value); | |
$parsed: null; | |
@if 'map' == $type { | |
$parsed: (); | |
@each $key, $val in $value { | |
$parsed: map-merge($parsed, ('#{$key}': data__parse-value($val))); | |
} | |
} | |
@else if 'list' == $type { | |
$parsed: (); | |
$sep: list-separator($value); | |
@each $val in $value { | |
$parsed: append($parsed, data__parse-value($val), $sep); | |
} | |
} | |
@else if 'string' == $type { | |
$parsed: data__parse-string($value); | |
} | |
@else { | |
$parsed: $value; | |
} | |
@return (type: $type, values: $parsed); | |
} | |
@function data__parse-string ($input) { | |
$parsed: (null); // hack to force list wrapping single map | |
$rest: $input; | |
$index: str-index($rest, '${'); | |
@if $index { | |
@if $index > 1 { | |
$parsed: append($parsed, str-slice($rest, 1, $index - 1)); | |
} | |
$close: -1; | |
@while $index { | |
$close: str-index($rest, '}'); | |
@if $close < $index { | |
@error 'unexpected `}`'; | |
} | |
$path: str-slice($rest, $index + 2, $close - 1); | |
@while str-slice($path, 1, 1) == ' ' { | |
$path: str-slice($path, 2); | |
} | |
@while str-slice($path, str-length($path), str-length($path)) == ' ' { | |
$path: str-slice($path, 1, str-length($path) - 1); | |
} | |
$parsed: append($parsed, (path: $path)); | |
$rest: str-slice($rest, $close + 1); | |
$index: str-index($rest, '${'); | |
@if $index and $index > 1 { | |
$parsed: append($parsed, str-slice($rest, 1, $index - 1)); | |
} | |
} | |
@if '' != $rest { | |
$parsed: append($parsed, $rest); | |
} | |
} | |
@else { | |
$parsed: append($parsed, $rest); | |
} | |
@return $parsed; | |
} | |
@function data__render-value($data) { | |
@if map != type-of($data) { | |
@return $data; | |
} | |
$render: null; | |
$type: map-get($data, 'type'); | |
$values: map-get($data, 'values'); | |
@if map == $type or 'root' == $type { | |
$render: (); | |
@each $key, $val in $values { | |
$render: map-merge($render, ($key: data__render-value($val))); | |
} | |
} | |
@else if 'list' == $type { | |
$render: (); | |
$sep: list-separator($values); | |
@each $val in $values { | |
$render: append($render, data__render-value($val), $sep); | |
} | |
} | |
@else if 'string' == $type { | |
@if length($values) > 2 { | |
@each $val in $values { | |
@if map == type-of($val) { | |
$render: "#{$render}#{data-get(map-get($val, 'path'))}"; | |
} | |
@else { | |
$render: "#{$render}#{$val}"; | |
} | |
} | |
} | |
@else if 'map' == type-of(nth($values, 2)) { | |
$render: data-get(map-get(nth($values, 2), 'path')); | |
} | |
@else { | |
$render: nth($values, 2); | |
} | |
} | |
@else { | |
$render: $values; | |
} | |
@return $render; | |
} | |
set-root-map { | |
@if data('preserved', true) {} | |
@if data(( | |
foo: 'bar', | |
baz: 'qux' | |
)) {} | |
preserved: data('preserved'); | |
foo: data('foo'); | |
baz: data('baz'); | |
} | |
map-keypath { | |
@if data(( | |
foo: (baz: 'bar', 'deep.keypath': 'yes'), | |
'foo.baz': 'qux' | |
)) {} | |
foo: map-get(data('foo'), 'baz'); | |
foo-baz: data('foo.baz'); | |
deep: data('foo.deep.keypath'); | |
} | |
types { | |
@if | |
data('string', 'string') | |
data('keyword', keyword) | |
data('number', 1664) | |
data('list', 'string' keyword 1664) | |
data('comma', ('string', keyword, 1664)) | |
data('map', ( | |
string: 'string', | |
keyword: keyword, | |
number: 1664, | |
list: 'string' keyword 1664 | |
)){} | |
string: data('string'); | |
keyword: data('keyword'); | |
number: data('number'); | |
/* list */ | |
list: data('list'); | |
list-comma: data('comma'); | |
/* map */ | |
map-string: data('map.string'); | |
map-keyword: data('map.keyword'); | |
map-number: data('map.number'); | |
map-list: data('map.list'); | |
} | |
keyword-path { | |
@if | |
data(( | |
red: 'is dead', | |
inherit: 'in gattaca' | |
)) {} | |
red: data('red'); | |
inherit: data('inherit'); | |
} | |
interpolation { | |
@if | |
data(( | |
'root': 'ROOT', | |
'sub.element': 'SUB', | |
'dyn': 'DYN1', | |
'keyword': bottom, | |
'tests': | |
'${ dyn }' | |
'${ root }' | |
'${ sub.element }' | |
'${ keyword }' | |
(top right '${ keyword }') | |
'${ root }--' | |
'--${ root }' | |
'${ root }${ sub.element }' | |
'${ root }--${ sub.element }' | |
'${ root }--${ sub.element }--' | |
'--${ root }--${ sub.element }' | |
'--${ root }--${ sub.element }--' | |
)) {} | |
test: nth(data('tests'), 1); | |
@if data('dyn', 'DYN2') {} | |
$tests: data('tests'); | |
@each $test in $tests { | |
test: $test; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set-root-map { | |
preserved: true; | |
foo: "bar"; | |
baz: "qux"; | |
} | |
map-keypath { | |
foo: "qux"; | |
foo-baz: "qux"; | |
} | |
types { | |
string: "string"; | |
keyword: keyword; | |
number: 1664; | |
/* list */ | |
list: "string" keyword 1664; | |
list-comma: "string", keyword, 1664; | |
/* map */ | |
map-string: "string"; | |
map-keyword: keyword; | |
map-number: 1664; | |
map-list: "string" keyword 1664; | |
} | |
keyword-path { | |
red: "is dead"; | |
inherit: "in gattaca"; | |
} | |
interpolation { | |
test: "DYN1"; | |
test: "DYN2"; | |
test: "ROOT"; | |
test: "SUB"; | |
test: bottom; | |
test: top right bottom; | |
test: "ROOT--"; | |
test: "--ROOT"; | |
test: "ROOTSUB"; | |
test: "ROOT--SUB"; | |
test: "ROOT--SUB--"; | |
test: "--ROOT--SUB"; | |
test: "--ROOT--SUB--"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment