Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Commit

Permalink
Merge pull request #6 from PrisisForks/master
Browse files Browse the repository at this point in the history
Some bug fixes, added test for set and get, more test coming
  • Loading branch information
prisis committed Jul 2, 2015
2 parents 82c5637 + 2b88e80 commit 7e604b1
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 128 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "growcss-sass-config-manager",
"version": "0.0.0",
"version": "1.0.0",
"homepage": "http://growcss.com/",
"authors": [
"Daniel Bannert <d.bannert@anolilab.de>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "growcss-sass-config-manager",
"version": "0.0.0",
"version": "1.0.0",
"description": "A dot-syntax configuration (Map) library for Sass (mixin / function).",
"author": {
"name": "Daniel Bannert",
Expand Down
Empty file removed src/scss/base/.gitkeep
Empty file.
9 changes: 4 additions & 5 deletions src/scss/config-manager.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Here are all the functions, helpers, mixins and variables.
@import
'utils/variables',
'utils/functions',
'utils/helpers',
'utils/mixins';
@import 'utils/variables';
@import 'utils/helpers';
@import 'utils/functions';
@import 'utils/mixins';
125 changes: 22 additions & 103 deletions src/scss/utils/_functions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// @return {*} The value of the configuration path
@function config-set($key, $value, $default: false) {
@if $default {
@if config-map-has($config-default, $path) {
$value: config-map-get($config-default, $path);
@if config-map-has($config-default, $key) {
$value: config-map-get($config-default, $key);
} @else {
$config-default: config-map-set($config-default, $path, $value) !global;
$config-default: config-map-set($config-default, $key, $value) !global;
}
} @else {
$config-attr: config-map-set($config-attr, $path, $value) !global;
$config-attr: config-map-set($config-attr, $key, $value) !global;
}

@return $value;
Expand All @@ -34,14 +34,14 @@
// @return {*} The value of the configuration path
@function config-get($key, $default: false) {
@if $default {
@if config-map-has($config-default, $path) {
@return config-map-get($config-default, $path);
@if config-map-has($config-default, $key) {
@return config-map-get($config-default, $key);
}
} @else {
@if config-map-has($config-attr, $path) {
@return config-map-get($config-attr, $path);
} @else if config-map-has($config-default, $path) {
@return config-map-get($config-default, $path);
@if config-map-has($config-attr, $key) {
@return config-map-get($config-attr, $key);
} @else if config-map-has($config-default, $key) {
@return config-map-get($config-default, $key);
}
}

Expand All @@ -54,7 +54,7 @@
//
// @function config-bind
//
// @param $path {string} The configuration path
// @param $key {string} The configuration path
// @param $value {array} The value to set
// @param $default {bool} Whether the configuration is default
//
Expand All @@ -64,114 +64,33 @@
@return config-set($key + $config-delimiter + $value, $value, $default);
}

@return config-get($path, $default);
@return config-get($key, $default);
}

// Returns whether a configuration path exists
//
// @function config-has
//
// @param $path {string} The configuration path
// @param $key {string} The configuration path
//
// @return {bool} True if the configuration path has a value,
// @return {bool} True if the configuration path has a value,
// otherwise false
@function config-has($path) {
@if config-map-has($config-attr, $path) or config-map-has($config-default, $path) {
@function config-has($key) {
@if config-map-has($config-attr, $key) or config-map-has($config-default, $key) {
@return true;
}

@return false;
}

// Returns whether the key of a map path exists in a Map
//
// @function config-map-has
//
// @private
//
// @param $map {map} The Map
// @param $path {string} The map path
//
// @return {bool} True if the map path has a value,
// otherwise false
@function config-map-has($map, $path) {
$keys: str-split($path, $config-delimiter);
$value: $map;

@each $key in $keys {
@if type-of($value) != 'map' or not map-has-key($value, $key) {
@return false;
}

$value: map-get($value, $key);
}

@return true;
}

// Gets a value from a Map by the map path
// Removes keys from maps
//
// @function config-map-get
// @function config-remove
//
// @private
// @param $key {string} The configuration path
//
// @param $map {map} The Map
// @param $path {string} The map path
//
// @return {*} The value of the map path
@function config-map-get($map, $path) {
$keys: str-split($path, $config-delimiter);
$value: $map;

@each $key in $keys {
@if type-of($value) != 'map' or not map-has-key($value, $key) {
@warn 'The path "#{$path}" doesn\'t exist.';

@return null;
}

$value: map-get($value, $key);
}

@return $value;
}

// Sets a value to a Map by the map path
//
// @function config-map-set
//
// @private
//
// @param $map {map} The Map
// @param $path {string} The map path
// @param $value {*|null} The value to set
//
// @return {map} A new Map
@function config-map-set($map, $path, $value) {
$keys: str-split($path, $config-delimiter);
$length: length($keys);
$result: (nth($keys, $length): $value);

@for $i from 1 through $length - 1 {
$j: $length - $i;
$key: nth($keys, $j);

$path: str-join(list-slice($keys, 1, $j), $config-delimiter);

@if config-map-has($map, $path) {
$value: config-map-get($map, $path);

@if type-of($value) == 'map' {
$result: config-map-merge(($key: $value), ($key: $result));
} @else {
$result: ($key: $result);
}
} @else {
$result: ($key: $result);
}
}

$map: config-map-merge($map, $result);
// @return {bool} True if the configuration path has a value,
// otherwise false
@function config-remove($key) {

@return $map;
}
111 changes: 105 additions & 6 deletions src/scss/utils/_helpers.scss
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,112 @@
//
// @return {list} The extracted list
@function list-slice($list, $start: 1, $end: length($list)) {
$result: ();
$output: ();

@for $i from $start through $end {
$result: append($result, nth($list, $i));
$output: append($output, nth($list, $i));
}

@return $result;
@return $output;
}

// Returns whether the key of a map path exists in a Map
//
// @function config-map-has
//
// @private
//
// @param $map {map} The Map
// @param $key {string} The map path
//
// @return {bool} True if the map path has a value,
// otherwise false
@function config-map-has($map, $key) {
$keys: str-split($key, $config-delimiter);
$value: $map;

@each $key in $keys {
@if type-of($value) != 'map' or not map-has-key($value, $key) {
@return false;
}

$value: map-get($value, $key);
}

@return true;
}

// Gets a value from a Map by the map path
//
// @function config-map-get
//
// @private
//
// @param $map {map} The Map
// @param $path {string} The map path
//
// @return {*} The value of the map path
@function config-map-get($map, $path) {
$keys: str-split($path, $config-delimiter);
$value: $map;

@each $key in $keys {
@if type-of($value) != 'map' or not map-has-key($value, $key) {
@warn 'The path "#{$path}" doesn\'t exist.';

@return null;
}

$value: map-get($value, $key);
}

@return $value;
}

// Sets a value to a Map by the map path
//
// @function config-map-set
//
// @private
//
// @param $map {map} The Map
// @param $path {string} The map path
// @param $value {*|null} The value to set
//
// @return {map} A new Map
@function config-map-set($map, $path, $value) {
$keys: str-split($path, $config-delimiter);
$length: length($keys);

$result: (nth($keys, $length): $value);

@for $i from 1 through $length - 1 {
$path: '';
$key: nth($keys, 1);

@if $length > 1 {
$j: $length - $i;
$key: nth($keys, $j);

$path: str-join(list-slice($keys, 1, $j), $config-delimiter);
}

@if config-map-has($map, $path) {
$value: config-map-get($map, $path);

@if type-of($value) == 'map' {
$result: config-map-merge(($key: $value), ($key: $result));
} @else {
$result: ($key: $result);
}
} @else {
$result: ($key: $result);
}
}

$map: config-map-merge($map, $result);

@return $map;
}

// Recursively merges one or more maps
Expand All @@ -95,10 +194,10 @@
//
// @private
//
// @param $maps {map} The map(s) to merge
// @param $maps {map...} The map(s) to merge
//
// @return {map} The merged map
@function config-map-merge($maps) {
// @return {map} The merged map
@function config-map-merge($maps...) {
$result: nth($maps, 1);

@for $i from 1 through length($maps) - 1 {
Expand Down
5 changes: 2 additions & 3 deletions tasks/aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
module.exports = {
'test' : [
'scsslint',
'mochacli',
'mochacli'
],
'default': [
'test',
'sass_import'
'test'
]
};
18 changes: 10 additions & 8 deletions tasks/sass_import.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
//sass_import config

module.exports = {
options: {
basePath: 'src/scss/'
dev: {
options: {
basePath: ''
},
files: {
'tests/specs/include.scss': [
'src/scss/utils/*',
'tests/specs/scss/*'
]
}
},
files: {
'config-manager.scss': [
'base/*',
'utils/*'
]
}
};
4 changes: 4 additions & 0 deletions tests/specs/include.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import 'src/scss/utils/functions';
@import 'src/scss/utils/helpers';
@import 'src/scss/utils/mixins';
@import 'src/scss/utils/variables';
2 changes: 1 addition & 1 deletion tests/specs/sass.mocha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

var path = require('path'),
sassTrue = require('sass-true'),
sassFile = path.join(__dirname, 'scss/tests.scss');
sassFile = path.join(__dirname, 'tests.scss');

sassTrue.runSass({file: sassFile}, describe, it);
Loading

0 comments on commit 7e604b1

Please sign in to comment.