Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ncoden committed Dec 15, 2016
2 parents 0a5fc57 + de4490d commit 51a3acb
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 1 deletion.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# sass-types
# Sass Types
✨ Awesome Type Checking in Sass

### Install with NPM
```
npm install ncoden/sass-types
```

:warning: This Sass module is still in development.

### Usage

```scss
@import 'sass-types';

@function my-function($number, $string) {
$_: st-assert('my-function', (number $number, string $string));

...
}
```

Version 0.2.0 - MIT License.
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "sass-types",
"version": "0.2.0",
"description": "✨ Awesome Type Checking in Sass",
"main": " ",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ncoden/sass-types.git"
},
"author": "Nicolas Coden <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/ncoden/sass-types/issues"
},
"homepage": "https://github.com/ncoden/sass-types#readme"
}
13 changes: 13 additions & 0 deletions scss/sass-types.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Helpers
@import 'sass-types/helpers/error';

// Default types
@import 'sass-types/types/default';

// API
@import 'sass-types/api/add-type';
@import 'sass-types/api/assert';
@import 'sass-types/api/check-type';
@import 'sass-types/api/init';

$_: st-init();
18 changes: 18 additions & 0 deletions scss/sass-types/api/_add-type.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

@function st-add-type($type, $func, $args: (), $deps: ()) {
@if type-of($type) == 'list' {
@each $t in $type {
$_: st-add-type($t, $func, $args, $deps);
}
}
@else if type-of($type) == 'string' {
$-st-test-functions: map-merge($-st-test-functions, ($type:
(
func_name: $func,
func_args: $args,
deps: $deps
)
)) !global;
}
@return null;
}
21 changes: 21 additions & 0 deletions scss/sass-types/api/_assert.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

@function st-assert($msg, $args) {
$pass_all: true;

@each $arg in $args {
$type: nth($arg, 1);
$value: nth($arg, 2);

$pass: st-check-type($value, $type);
@if not $pass {
$pass_all: false;
@if $pass == null {
$_: -st-throw-error('Unknown type "' + $type + '"', $msg);
}
@else if $pass == false {
$_: -st-throw-error('Wrong type "' + $type + '"', $msg);
}
}
}
@return $pass_all;
}
19 changes: 19 additions & 0 deletions scss/sass-types/api/_check-type.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

@function st-check-type($value, $type) {
$type_datas: map-get($-st-test-functions, $type);
@if ($type_datas == null) {
@return null;
}

$func_name: map-get($type_datas, 'func_name');
$func_args: map-get($type_datas, 'func_args');
$deps: map-get($type_datas, 'deps');

@each $dep in $deps {
@if not st-check-type($value, $dep) {
@return false;
}
}
$pass: call($func_name, $value, $func_args...);
@return $pass;
}
9 changes: 9 additions & 0 deletions scss/sass-types/api/_init.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

@function st-init($default_types: true) {
$-st-test-functions: () !default !global;

@if $default_types == true {
$_: st-add-default-types();
}
@return null;
}
11 changes: 11 additions & 0 deletions scss/sass-types/helpers/_error.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

@function -st-throw-error($error, $prefix: null) {
$msg: '';
@if $prefix != null {
$msg: $msg + $prefix + ': ';
}
$msg: $msg + $error;
@error $msg;

@return null;
}
24 changes: 24 additions & 0 deletions scss/sass-types/types/_default.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

@function st-add-default-types() {
$_: st-add-type(('map', 'Map'), -st-test-map);
$_: st-add-type(('list', 'List'), -st-test-type, ('list'));
$_: st-add-type(('string', 'String'), -st-test-type, ('string'));
$_: st-add-type(('number', 'Number'), -st-test-type, ('number'));
$_: st-add-type(('bool', 'Boolean'), -st-test-type, ('bool'));
$_: st-add-type(('color', 'Color'), -st-test-type, ('color'));
@return null;
}

@function -st-test-type($value, $type) {
@return type-of($value) == $type;
}

@function -st-test-map($value) {
@if type-of($value) == 'map' {
@return true;
}
@else if type-of($value) == 'list' and length($value) == 0 {
@return true;
}
@return false;
}

0 comments on commit 51a3acb

Please sign in to comment.