// Adds $value at $index in $list // ------------------------------------------------------------------------------- // @documentation http://sassylists.com/documentation/#insert-nth // ------------------------------------------------------------------------------- // @example insert-nth(a b c, 2, z) => a z d c // @example insert-nth(a b c, 0, z) => error // @example insert-nth(a b c, -1, z) => error // @example insert-nth(a b c, 10, z) => a b c z // ------------------------------------------------------------------------------- // @param $list [List] : list // @param $index [Number] : index to add // @param $value [Literal] : value to add // ------------------------------------------------------------------------------- // @raise [Error] if $index isn't an integer // @raise [Error] if $index is strictly lesser than 1 // @raise [Error] if $index is strictly greater than length of $list // ------------------------------------------------------------------------------- // @return [List] | false @function insert-nth($list, $index, $value) { $result: false; @if type-of($index) != number { @warn "List index #{$index} is not a number for `insert-nth`."; @return $result; } @else if $index < 1 { @warn "List index #{$index} must be a non-zero integer for `insert-nth`"; @return $result; } @else if $index > length($list) { @return append($list, $value); } @else { $result: (); @for $i from 1 through length($list) { @if $i == $index { @if $value and $value != "" and $value != () { $result: append($result, $value); } } $result: append($result, nth($list, $i)); } } @return $result; }