-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
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. |
This file contains 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
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" | ||
} |
This file contains 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
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(); |
This file contains 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
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; | ||
} |
This file contains 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
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; | ||
} |
This file contains 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
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; | ||
} |
This file contains 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
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; | ||
} |
This file contains 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
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; | ||
} |
This file contains 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
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; | ||
} |