Last active
August 29, 2015 14:05
-
-
Save mmintel/701af0b487cd511a04b2 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
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
// ---- | |
// Sass (v3.4.1) | |
// Compass (v1.0.1) | |
// ---- | |
/** | |
* Tests whether `$var` is bool. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for bool type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_bool(true) //returns true | |
* is_bool(false) //returns true | |
* is_bool(test) //returns false | |
* is_bool(1) //returns false | |
* is_bool(null) //returns false | |
*/ | |
@function is_bool($var) { | |
@if type-of($var) == bool { | |
@return true; | |
} @else { | |
@return false; | |
} | |
} | |
/** | |
* Tests whether `$var` is color. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for color type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_color(#333) //returns true | |
* is_color(black) //returns true | |
* is_color(test) //returns false | |
*/ | |
@function is_color($var) { | |
@if type-of($var) == color { | |
@return true; | |
} @else { | |
@return false; | |
} | |
} | |
/** | |
* Tests whether `$var` is empty. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for emptiness | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_empty('') //returns true | |
* is_empty(()) //returns true | |
* is_empty('test') //returns false | |
*/ | |
@function is_empty($var) { | |
@if type-of($var) == string { | |
@return str-length($var) == 0; | |
} | |
@if type-of($var) == number { | |
@return $var != 0; | |
} | |
@if type-of($var) == list { | |
@return length($var) == 0; | |
} | |
@if $var == false { | |
@return true; | |
} | |
@if $var == null { | |
@return true; | |
} | |
} | |
/** | |
* Tests whether `$var` is list. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for list type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_list(a b) //returns true | |
* is_list(1 2 3) //returns true | |
* is_list((test, test)) //returns true | |
* is_list(test) //returns false | |
* is_list(1) //returns false | |
*/ | |
@function is_list($var) { | |
@if type-of($var) == list { | |
@return true; | |
} | |
@else { | |
@return false; | |
} | |
} | |
/** | |
* Tests whether `$var` is measure. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for measure type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_measure(1px) //returns true | |
* is_measure(5em) //returns true | |
* is_measure(5) //returns false | |
* is_measure(test) //returns false | |
* is_measure(em) //returns false | |
*/ | |
@function is_measure($var) { | |
@if type-of($var) == number { | |
@if unitless($var) { | |
@return false; | |
} | |
} | |
@else { | |
@return false; | |
} | |
@if not index($UNITS, unit($var)) { | |
@return false; | |
} | |
@else { | |
@return true; | |
} | |
} | |
/** | |
* Tests whether `$var` is bool. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for bool type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_null(null) //returns true | |
* is_null(false) //returns false | |
* is_null(0) //returns false | |
*/ | |
@function is_null($var) { | |
@if $var == null { | |
@return true; | |
} @else { | |
@return false; | |
} | |
} | |
/** | |
* Tests whether `$var` is numeric. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for numeric type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_numeric(1) //returns true | |
* is_numeric('1') //returns false | |
* is_numeric(1em) //returns false | |
*/ | |
@function is_numeric($var) { | |
@if type-of($var) == number { | |
@return unitless($var); | |
} | |
@else { | |
@return false; | |
} | |
} | |
/** | |
* Tests whether `$var` is string. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for string type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_string(test) //returns true | |
* is_string(1) //returns false | |
*/ | |
@function is_string($var) { | |
@if type-of($var) != string { | |
@return false; | |
} @else { | |
@return true; | |
} | |
} | |
/** | |
* Tests whether `$var` is unit. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @requires invoke | |
* --- | |
* @param {*} $value - string or list to test | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_empty('') //returns true | |
* is_empty(()) //returns true | |
* is_empty('test') //returns false | |
*/ | |
@function is_unit($var) { | |
@if type-of($var) == string { | |
@return if(index($UNITS, $var), true, false); | |
} @else { | |
@return false; | |
} | |
} | |
/** | |
* validates input against various tests | |
* --- | |
* @requires is_bool | |
* @requires is_color | |
* @requires is_empty | |
* @requires is_list | |
* @requires is_measure | |
* @requires is_null | |
* @requires is_numeric | |
* @requires is_string | |
* @requires is_unit | |
* --- | |
* @param {value} $var - a variable to be tested | |
* @param {list|string|any} $validations - the validations to test (e.g. measure, string, number, unit). "any" means it can be any type so there won't be any kind of test. | |
* --- | |
* @return {bool} | |
* --- | |
* @throws '$var is not a known validation type.' | |
* --- | |
* @example | |
* validate(1em, measure) //returns 1em | |
* validate(em, unit string) //returns em | |
* validate(1, number string) //returns 1 | |
* validate(1, number) //returns 1 | |
* validate(1, string) //returns false | |
* --- | |
* @since 1.0.0 | |
*/ | |
@function validate($var, $validations) { | |
//set a list of testable validation types | |
$allTypes: measure, list, bool, string, color, number, unit; | |
//do not test anything if type equals "any" | |
@if $validations == any { | |
@return true; | |
} | |
//if there is more than one validation test | |
@if length($validations) > 1 { | |
//break function if validation is not a known data type | |
@each $validation in $validations { | |
@if not index($allTypes, $validation) { | |
@warn '#{$validation} is not a known validation type.'; | |
@return false; | |
} | |
} | |
} | |
//collect validation errors | |
$errors: (); | |
//check each validation and set 1 for each failure | |
@each $validation in $validations { | |
@if $validation == measure { | |
@if not is_measure($var) { | |
$errors: append($errors, 1); | |
} | |
} | |
@else if $validation == list { | |
@if not is_list($var) { | |
$errors: append($errors, 1); | |
} | |
} | |
@else if $validation == color { | |
@if not is_color($var) { | |
$errors: append($errors, 1); | |
} | |
} | |
@else if $validation == bool { | |
@if not is_bool($var) { | |
$errors: append($errors, 1); | |
} | |
} | |
@else if $validation == null { | |
@if not is_null($var) { | |
$errors: append($errors, 1); | |
} | |
} | |
@else if $validation == string { | |
@if not is_string($var) { | |
$errors: append($errors, 1); | |
} | |
} | |
@else if $validation == number { | |
@if not is_numeric($var) { | |
$errors: append($errors, 1); | |
} | |
} | |
@else if $validation == unit { | |
@if not is_unit($var) { | |
$errors: append($errors, 1); | |
} | |
} | |
//as we checked earlier $validation must be one of our data types | |
//so this condition my never be encountered but just in case..return false | |
@else { | |
@return false; | |
} | |
} | |
//throw an error if there are as many validation errors as validation tests because all checked types failed | |
@if length($errors) >= length($validations) { | |
@return false; | |
} | |
//at least one of tested types must be valid | |
@else { | |
@return true; | |
} | |
} | |
//this example is for you to try | |
@function test($test) { | |
@if validate($test, string) { | |
@return true; | |
} @else { | |
@return false; | |
} | |
} |
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
@charset "UTF-8"; | |
/** | |
* Tests whether `$var` is bool. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for bool type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_bool(true) //returns true | |
* is_bool(false) //returns true | |
* is_bool(test) //returns false | |
* is_bool(1) //returns false | |
* is_bool(null) //returns false | |
*/ | |
/** | |
* Tests whether `$var` is color. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for color type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_color(#333) //returns true | |
* is_color(black) //returns true | |
* is_color(test) //returns false | |
*/ | |
/** | |
* Tests whether `$var` is empty. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for emptiness | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_empty('') //returns true | |
* is_empty(()) //returns true | |
* is_empty('test') //returns false | |
*/ | |
/** | |
* Tests whether `$var` is list. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for list type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_list(a b) //returns true | |
* is_list(1 2 3) //returns true | |
* is_list((test, test)) //returns true | |
* is_list(test) //returns false | |
* is_list(1) //returns false | |
*/ | |
/** | |
* Tests whether `$var` is measure. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for measure type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_measure(1px) //returns true | |
* is_measure(5em) //returns true | |
* is_measure(5) //returns false | |
* is_measure(test) //returns false | |
* is_measure(em) //returns false | |
*/ | |
/** | |
* Tests whether `$var` is bool. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for bool type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_null(null) //returns true | |
* is_null(false) //returns false | |
* is_null(0) //returns false | |
*/ | |
/** | |
* Tests whether `$var` is numeric. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for numeric type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_numeric(1) //returns true | |
* is_numeric('1') //returns false | |
* is_numeric(1em) //returns false | |
*/ | |
/** | |
* Tests whether `$var` is string. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @param {*} $var - check for string type | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_string(test) //returns true | |
* is_string(1) //returns false | |
*/ | |
/** | |
* Tests whether `$var` is unit. | |
* --- | |
* @access public | |
* --- | |
* @since 1.0.0 | |
* --- | |
* @requires invoke | |
* --- | |
* @param {*} $value - string or list to test | |
* --- | |
* @return {bool} | |
* --- | |
* @example | |
* is_empty('') //returns true | |
* is_empty(()) //returns true | |
* is_empty('test') //returns false | |
*/ | |
/** | |
* validates input against various tests | |
* --- | |
* @requires is_bool | |
* @requires is_color | |
* @requires is_empty | |
* @requires is_list | |
* @requires is_measure | |
* @requires is_null | |
* @requires is_numeric | |
* @requires is_string | |
* @requires is_unit | |
* --- | |
* @param {value} $var - a variable to be tested | |
* @param {list|string|any} $validations - the validations to test (e.g. measure, string, number, unit). "any" means it can be any type so there won't be any kind of test. | |
* --- | |
* @return {bool} | |
* --- | |
* @throws '$var is not a known validation type.' | |
* --- | |
* @example | |
* validate(1em, measure) //returns 1em | |
* validate(em, unit string) //returns em | |
* validate(1, number string) //returns 1 | |
* validate(1, number) //returns 1 | |
* validate(1, string) //returns false | |
* --- | |
* @since 1.0.0 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment