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 #7 from PrisisForks/master
Browse files Browse the repository at this point in the history
Added more test, remove old functions, fixed some bugs
  • Loading branch information
prisis committed Jul 5, 2015
2 parents 7e604b1 + f8058ab commit cf6c27b
Show file tree
Hide file tree
Showing 18 changed files with 368 additions and 341 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Author](http://img.shields.io/badge/author-@@anolilab-blue.svg?style=flat-square)](https://twitter.com/@anolilab)
[![npm](https://img.shields.io/npm/v/sass-config-manager.svg?style=flat-square)](https://www.npmjs.com/package/sass-config-manager)
[![David](https://img.shields.io/david/growcss/sass-config-manager.svg?style=flat-square)](https://david-dm.org/growcss/sass-config-manager#info=dependencies&view=table)
[![devDependency Status](https://david-dm.org/growcss/sass-config-manager/dev-status.svg?style=flat-square)](https://david-dm.org/growcss/sass-config-manager#info=devDependencies)
[![npm](https://img.shields.io/npm/v/npm.svg?style=flat-square)](https://www.npmjs.com/package/sass-config-manager)
[![GitHub release](https://img.shields.io/github/release/qubyte/rubidium.svg?style=flat-square)](https://github.com/growcss/sass-config-manager/releases)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
Expand Down
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": "1.0.0",
"version": "2.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": "1.0.0",
"version": "2.0.0",
"description": "A dot-syntax configuration (Map) library for Sass (mixin / function).",
"author": {
"name": "Daniel Bannert",
Expand Down
7 changes: 5 additions & 2 deletions src/scss/config-manager.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Here are all the functions, helpers, mixins and variables.
@import 'utils/variables';
@import 'utils/helpers';
@import 'utils/functions';

@import 'helpers/helpers';

@import 'functions/functions';

@import 'utils/mixins';
25 changes: 25 additions & 0 deletions src/scss/functions/_config-get.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Gets a value to/from a configuration path
//
// @function config-get
//
// @param $key {string} The configuration path
// @param $default {bool} Whether the configuration is default
//
// @return {*} The value of the configuration path
@function config-get($key, $default: false) {
@if $default {
@if config-map-has($config-default, $key) {
@return config-map-get($config-default, $key);
}
} @else {
@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);
}
}

@warn 'The key "#{$key}" doesn\'t exist.';

@return null;
}
15 changes: 15 additions & 0 deletions src/scss/functions/_config-has.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Returns whether a configuration path exists
//
// @function config-has
//
// @param $key {string} The configuration path
//
// @return {bool} True if the configuration path has a value,
// otherwise false
@function config-has($key) {
@if config-map-has($config-attr, $key) or config-map-has($config-default, $key) {
@return true;
}

@return false;
}
22 changes: 22 additions & 0 deletions src/scss/functions/_config-set.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Sets a value to/from a configuration path
//
// @function config
//
// @param $key {string} The configuration key
// @param $value {*} The value to set
// @param $default {bool} Whether the configuration is default
//
// @return {*} The value of the configuration path
@function config-set($key, $value, $default: false) {
@if $default {
@if config-map-has($config-default, $key) {
$value: config-map-get($config-default, $key);
} @else {
$config-default: config-map-set($config-default, $key, $value) !global;
}
} @else {
$config-attr: config-map-set($config-attr, $key, $value) !global;
}

@return $value;
}
3 changes: 3 additions & 0 deletions src/scss/functions/_functions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import 'config-set';
@import 'config-get';
@import 'config-has';
3 changes: 3 additions & 0 deletions src/scss/helpers/_helpers.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import 'list';
@import 'str';
@import 'map';
39 changes: 39 additions & 0 deletions src/scss/helpers/_list.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Return list-map from `$list` and ensure input list-map is list-of-lists
//
// @function list-map-check
//
// @access public
//
// @param $list {list}
//
// @return {list-map}
@function list-map-check($list) {
@if length($list) == 2 and length(nth($list, 1)) == 1 {
@return append((), $list, 'comma');
}

@return $list;
}

// Extracts a slice of a list
//
// @function list-slice
//
// @access private
//
// @param $list {list} The list to extract
// @param $start {number} The start index to extract
// @param $end {number} The end index to extract
//
// @return {list} The extracted list
@function list-slice($list, $start: 1, $end: length($list)) {
$output: ();

@if $start >= 1 and $end >= $start {
@for $i from $start through $end {
$output: append($output, nth($list, $i));
}
}

@return $output;
}
130 changes: 130 additions & 0 deletions src/scss/helpers/_map.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Sets a value to a Map by the map path
//
// @function config-map-set
//
// @access 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) {
$map: list-map-check($map);
$keys: str-split($path, $config-delimiter);
$length: length($keys);

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

@if $length > 1 {
@for $i from 1 through $length - 1 {
$path: '';
$key: nth($keys, 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;
}

// Gets a value from a Map by the map path
//
// @function config-map-get
//
// @access 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: list-map-check($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;
}

// Returns whether the key of a map path exists in a Map
//
// @function config-map-has
//
// @access 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: list-map-check($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;
}

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

@for $i from 1 through length($maps) - 1 {
@each $key, $value in nth($maps, $i + 1) {
@if type-of($result) != 'map' {
$result: ($key: $value);
}

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

@if $key != null {
$result: map-merge($result, ($key: $value));
}
}
}

@return $result;
}
66 changes: 66 additions & 0 deletions src/scss/helpers/_str.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Joins list elements with a string
//
// @function str-join
//
// @access private
//
// @param $list {list} The list to join
// @param $glue {string} The glue string to join list elements
//
// @return {string} The joined string
@function str-join($list, $glue: '') {
$result: '';

@if length($list) == 0 {
@return $result;
}

@if length($list) > 1 {
@for $i from 1 through length($list) - 1 {
$result: $result + nth($list, $i) + $glue;
}
}

$result: $result + nth($list, length($list));

@return $result;
}

// Splits a string by a delimiter
//
// @function str-split
//
// @access private
//
// @param $string {string} The string to split
// @param $delimiter {string} The boundary string to split the string
//
// @return {list} The splitted list
@function str-split($string, $delimiter: '') {
$result: ();
$length: str-length($string);

@if str-length($delimiter) == 0 {
@for $i from 1 through $length {
$result: append($result, str-slice($string, $i, $i));
}

@return $result;
}

$break: false;

@while not $break {
$index: str-index($string, $delimiter);

@if not $index or $index == 0 {
$break: true;
} @else {
$part: if($index != 1, str-slice($string, 1, $index - 1), '');
$result: append($result, $part);
$string: str-slice($string, $index + str-length($delimiter));
}
}

@return append($result, $string);
}
Loading

0 comments on commit cf6c27b

Please sign in to comment.