Skip to content

Instantly share code, notes, and snippets.

@jackie
Last active August 29, 2015 14:04
Show Gist options
  • Save jackie/0e2defe858b8734f15ec to your computer and use it in GitHub Desktop.
Save jackie/0e2defe858b8734f15ec to your computer and use it in GitHub Desktop.
Sass Slice
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (two three four five six) == (two three four five six)
sass/screen.scss:72 DEBUG: 1, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (three four five six) == (three four five six)
sass/screen.scss:73 DEBUG: 2, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (four) == four
sass/screen.scss:74 DEBUG: 3, false
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (four five six) == (four five six)
sass/screen.scss:75 DEBUG: 4, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (three four five six) == (three four five six)
sass/screen.scss:76 DEBUG: 5, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (three four five six) == (three four five six)
sass/screen.scss:77 DEBUG: 6, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (one two) == (one two)
sass/screen.scss:78 DEBUG: 7, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (one two three four) == (one two three four)
sass/screen.scss:79 DEBUG: 8, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (three four five six) == (three four five six)
sass/screen.scss:80 DEBUG: 9, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (one two three four five six) == (one two three four five six)
sass/screen.scss:81 DEBUG: 10, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: () == ()
sass/screen.scss:82 DEBUG: 11, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: (five six) == (five six)
sass/screen.scss:83 DEBUG: 12, true
sass/screen.scss:66 DEBUG: ----
sass/screen.scss:67 DEBUG: () == ()
sass/screen.scss:84 DEBUG: 13, true
@function slice($list, $offset: 1, $length: length($list)) {
$new: ();
$list-length: length($list);
@if $offset < 1 {
$offset: $length - abs($offset+1);
$offset: if($offset <= 0, 1, $offset);
}
@if $offset > $list-length or $length <= 0 {
@return ();
}
$length: $offset + ($length - 1);
@if $length > $list-length {
$length: $list-length; // normalize length value
}
@for $i from $offset through $length {
$new: append($new, nth($list, $i));
}
@return $new;
}
@function assertEquals($a, $b) {
@debug '----';
@debug $a unquote('==') $b;
@return $a == $b;
}
$list: one two three four five six;
@debug 1, assertEquals(slice($list, 2), (two three four five six));
@debug 2, assertEquals(slice($list, 3), (three four five six));
@debug 3, assertEquals(slice($list, 4, 1), (four));
@debug 4, assertEquals(slice($list, 4, 5), (four five six));
@debug 5, assertEquals(slice($list, 3, 6), (three four five six));
@debug 6, assertEquals(slice($list, 3, 8), (three four five six));
@debug 7, assertEquals(slice($list, 1, 2), (one two));
@debug 8, assertEquals(slice($list, 1, 4), (one two three four));
@debug 9, assertEquals(slice($list, -4), (three four five six));
@debug 10, assertEquals(slice($list, -10), (one two three four five six));
@debug 11, assertEquals(slice($list, 20), ());
@debug 12, assertEquals(slice($list, 5, 2), (five six));
@debug 13, assertEquals(slice($list, 5, -3), ());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment