JigSass Tools Maps

NPM version Build Status Dependency Status

Map helper functions

Installation

Using npm:

npm i -S jigsass-tools-maps

Usage

@import 'path/to/jigsass-tools-maps/index.scss'; should give you all you need.

jigsass-tools-maps provides the following utility functions:

jigsass-get
Get a value from nested maps
jigsass-set
Set a value in nested maps
jigsass-deep-has-key
Check if a map has a nested key
jigsass-merge-deep
Recursively assign values from multiple maps into a single map

License: MIT

General

functions

jigsass-get

@function jigsass-get($map, $keys...) { ... }

Description

Get key values from nested maps Nesting can go as deep as needed, where keys are separated by commas.

Parameters

parameterNameparameterDescriptionparameterTypeparameterDefault value
$map

Map

Mapnone
$keys

Key chain

Arglistnone

Returns

Any type —

The value stored in the last key provided.

Throws

  • jigsass-get: #{$map} is a #{type-of($map)}, not a map.

Example

// scss
$typography: (
  h1: (
    size: 36px;
    weight: 700;
  );
);

.alpha {
  font-size: js-get($typography, h1, size);
  font-weight: js-get($typography, h1, weight);
}
// css ouput
.alpha{
  font-size: 36px;
  font-weight: 700;
}

jigsass-set

@function jigsass-set($map, $keys-value......, $value) { ... }

Description

Set a value of a key in a nested map. Nesting can go as deep as needed.

Parameters

parameterNameparameterDescriptionparameterTypeparameterDefault value
$map

The parent map in which keys and values will be set in.

Mapnone
$keys-value...

A comma-seperated list of keys to denote nesting hierarchy.

Arglistnone
$value

The value to assign to the last key in $keys

Anynone

Returns

Map

Throws

  • jigsass-set: #{$map} is a #{type-of($map)}, not a map.

Example

// Input:
$map: (
  key1: (
    key2: (
      existing-key: existing-value,
    ),
  ),
);
$map: jigsass-set($map, key1, key2, new-key, new-value);

// Outcome:
$map: (
  key1: (
    key2: (
      existing-key: existing-value,
      new-key: new-value,
    ),
  ),
);

jigsass-deep-has-key

@function jigsass-deep-has-key($map, $key...) { ... }

Description

Checks whether a map has a value associated with a given nested key.

Parameters

parameterNameparameterDescriptionparameterTypeparameterDefault value
$map

The map to search in

Mapnone
$key

the key hierarchy to look for

Arglistnone

Returns

Boolean

jigsass-deep-merge

@function jigsass-deep-merge($sources...) { ... }

Description

Recursively assign values from multiple maps into a single map.

Parameters

parameterNameparameterDescriptionparameterTypeparameterDefault value
$sources

Sources to be merged into a single $map.

Arglistnone

Returns

Map

Throws

  • jigsass-deep-merge: Only maps can be merged into,

  • jigsass-deep-merge: Only maps can be merged,