Skip to content

Instantly share code, notes, and snippets.

@Hokid
Last active August 29, 2015 14:13
Show Gist options
  • Save Hokid/b50c39ad0a35143461fb to your computer and use it in GitHub Desktop.
Save Hokid/b50c39ad0a35143461fb to your computer and use it in GitHub Desktop.
(sass) берет переменную и проверяет её тип данных с заданными. присутствует рекурсия
// ----
// Sass (v3.4.9)
// Compass (v1.0.1)
// ----
@function only-types( $var, $types, $rec: true ) {
$support-arg-types: string list;
$support-types: number string bool list color; // map, null
$var-type: type-of( $var );
$types-type: type-of( $types );
$var-len: length( $var );
// Валидация аргумента $types
@if not index( $support-arg-types, $types-type ) { @error "( $types: #{$types} )"; @return null }
@each $type in $types {
@if type-of( $type ) != string { @error "( $type: #{type-of($type)} )"; @return null }
@else if not index( $support-types, $type ) { @error "( $type: #{$type} )"; @return null }
}
// Валидация аргумента $rec
@if type-of( $rec ) != bool { @error "( $rec: #{$rec} )"; @return null }
// Валидация аргумента $var
@if $rec or $var-len == 1 { @if not index( $types, $var-type ) { @return false } }
@if $var-len > 1 {
@if $var-type == list {
@each $i in $var {
@if $rec { @if not only-types( $i, $types ) { @return false } }
@else { @if not index( $types, type-of( $i ) ) { @return false } }
}
@return true
}
} @else { @return true }
}
.b{
test: only-types(2 (2 true) 4, list number bool);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment