question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using quoted color strings as paramters with map-get returns null

See original GitHub issue

Stack Overflow: https://stackoverflow.com/questions/66018856/sass-string-to-non-string

I have a scenario where I am using a JSON file in order to dynamically create SASS variables. The plugin (Gulp-json-css) can retain the map and array properties, but does not add quotations onto the map keys:

$palettes : (color-1: #FF0000, white: #ffffff, grey: (lite: #aaaaaa, base: #999999, dark: #666666), black: #000000);

I am running into an issue where map-get() is not able to find the appropriate value if a quoted string is used for the parameter rather than using a unquoted “non-string”.

map-get($palettes, white) => #ffffff

map-get($palettes, "white") => null

Conclusion : white != "white"

Sure, you might also think that my conclusion obviously makes sense because white is a color.

BUT

The same issue occurs whenever I use the term grey as well, which is NOT a color. gray with an “a” is the spelling browsers use for their color code.

map-get($palettes, grey) => (lite: #aaaaaa, base: #999999, dark: #666666)

map-get($palettes, "grey") => null

HOWEVER

What really blows my mind is that color-1 is working exactly as how I would expect the other two scenarios to work:

map-get($palettes, color-1) => #FF0000

map-get($palettes, "color-1") => #FF0000

Is this a bug with the Sass code base?

OR

Is there anyway to convert a string into a “non-string”? I need a way to turn “white” into white in order to appropriately retrieve the correct value using the map-get() function.


After doing a little more debugging, I have concluded that there is a bug when a color is used as a “non-string” or “unquoted” map key.

map-get($palettes, "color") => This will always produce null 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mirisuzannecommented, Feb 3, 2021

Keys are allowed to be any data type – but you have to match the type when you set the key, and when you call it. If you use a color for the map key, you cant use a string to call it. The same is true in reverse. You’ll find that both grey & gray are valid color keywords (one since CSS1, the other since CSS3). When you add quotes to a color keyword, it becomes a string. But color-1 is a string with or without quotes - so there is no change in type.

$palettes: (
  gray: color,
  grey: another color,
  'gray': string,
  'grey': another string,
);

map-get($palettes, gray) => color
map-get($palettes, grey) => another color
map-get($palettes, "gray") => string
map-get($palettes, "grey") => another string
0reactions
mdurchholzcommented, Feb 5, 2021

I ended up writing a function that can loop through a map and add quotes to it’s keys. I have gotten everything working that I was caught up over.

@function quoteMapKeys( $map )
{
    $newMap : ();

    @each $key,$value in $map
    {
        @if type-of($value) == 'map'
        {
            $value : quoteMapKeys( $value );
        }

        $newMap : map-merge($newMap, (quote($key):$value));
    }

    @return $newMap;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using NULL as a key value in a Map - java - Stack Overflow
Let's say that we want color specific strings in a text, and we also want a default color for text which doesn't match...
Read more >
apex - SObject key of map mutated returns null but serializing ...
I am just wondering how you are even able to compare a boolean with string in assert statement and we don't get any...
Read more >
Maps - Sass
Most of the time, it's a good idea to use quoted strings rather than unquoted strings for map keys. This is because some...
Read more >
Return empty string instead of "null" with "jq --raw-output" ? #354
I'm doing something like this in a shell script: API_URL=http://whatever while [ "$API_URL" ]; do #....stuff.
Read more >
Data Types in Sass - SitePoint
An overview of the various data types available to developers in Sass, including numbers, booleans, strings, maps, lists and more.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found