Giter Site home page Giter Site logo

pentzzsolt / sass-recursive-map-merge Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 2.0 23 KB

A lightweight Sass function that merges multidimensional maps recursively.

License: MIT License

CSS 100.00%
sass sass-functions scss scss-functions scss-function sass-function

sass-recursive-map-merge's People

Contributors

pentzzsolt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

howardcox hangxun

sass-recursive-map-merge's Issues

Recusively join lists insde maps?

Hi, I was wondering if it was an intentional decision to not recursively join lists which are inside the maps being merged?

Eg:

@import "node_modules/sass-recursive-map-merge/_recursive-map-merge.scss";
$test1: (
	'a': (1,2,3),
);
$test2: (
	'a': (4,5,6),
);
@debug map-merge($test1, $test2);
@debug recursive-map-merge($test1, $test2);

I would expect to get the following from a recursive function:

(
	'a': (1,2,3,4,5,6),
);

But it just behaves the same as map-merge and keeps the second map's value.

I have a patch which will join two lists together if they have the same key, but if this was a deliberate design decision then I'll just fork and make my own function for this.

Wrong merge with empty list

So, sometimes we can have a source map which built with other custom maps. Sometimes some of the them may be an empty.

Data:

$map1: ( 
  x: (), 
  y: (2: 2) 
);

$map2: ( 
  x: (1: 1), 
  y: (3: 3) 
);

// Result
(
  x: (),
  y: (2: 2, 3: 3)
)

// Expected
(
  x: (1: 1),
  y: (2: 2, 3: 3)
);

Solution:

// Add small helper function 
@function convert-empty-list-to-map($source) {
  @if ( type-of($source) == list and length($source) == 0 ) {
    @return map-remove((x:x), x);
  }
  @return $source;
};

// Updated function
@function recursive-map-merge($source1, $source2 ) {
  @if ( type-of($source1) == map or ( type-of($source1) == list and length($source1) == 0) ) and
      ( type-of($source2) == map or ( type-of($source2) == list and length($source2) == 0) ) {
    
    // Check both maps and convert to [map] when empty 
    $map1: convert-empty-list-to-map($source1);
    $map2: convert-empty-list-to-map($source2);

    $result: $map1;

    @each $key, $value in $map2 {
      // Check both childs and convert to map when empty
      $map1-child: convert-empty-list-to-map(map-get($map1, $key));
      $map2-child: convert-empty-list-to-map($value);

      @if ( type-of($map1-child) == map and type-of($map2-child) == map ) {
        $result: map-merge($result, ($key: recursive-map-merge($map1-child, $map2-child)));
      }
      @else {
        $result: map-merge($result, ($key: $value));
      }
    }

    @return $result;
  }
  @else {
    @warn "recursive-map-merge() expects it\'s parameters to be map types!";
    @return null;
  }
}

Warning when using on empty maps

If at least one of the maps given to the function as an argument is empty, the function returns null and throws the warning:

recursive-map-merge() expects it's parameters to be map types!

This is because Sass sees empty maps as lists:

$empty-map: ();
if type-of($empty-map) == map {
	//This code is not executed.
}
if type-of($empty-map) == list {
	//This code is executed.
}

Because the function checks if both arguments are maps, it fails with empty maps.

how do I get a value from a map that's been recursively merged?

Hi,

This looks perfect for my current needs creating a cohesive taxonomy and set of variables to be used within a design system.

I think I have a couple of maps merged successfully -- how do I get the required value from the new map?

Here's the maps so far:

$extra-small: (
  'font-size': 12px,
  'font-weight': 400,
  'line-height': 1.4
);
$small: (
  'font-size': 14px,
  'font-weight': 300,
  'line-height': 1.5
);

$regular: (
  'font-size': 18px,
  'font-weight': 500,
  'line-height': 1.35,
);

$sans: recursive-map-merge(
  $extra-small,
  $small,
  $regular
);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.