oddbird / accoutrement

Combined Sass Accoutrement tools, with option for individual module imports
MIT License
37 stars 6 forks source link

a.has-token() function #48

Closed johncrim closed 2 years ago

johncrim commented 4 years ago

In addition to the existing token inspection functions, please consider adding a function for determining if a token is defined.

We're using SASS modules for our code, so all the members prefixed with _ (or -) are not accessible.

Here's an example that I think justifies this request. Starting with this example map:

@use 'accoutrement' as a;

$colors: (
  'form-field': (
    'active': (
      border: #2200ddaa,
      background: #ddd
    )
  )
);
@include a.add-colors($colors);

I want to be able to test whether a token exists:

.form-field {
  @if a.has-token(a.$colors, 'form-field->active->border') {
     &:focus {
        border: 2px solid a.color('form-field->active->border');
     }
  }
}

As a workaround I can use something like:

  @if a.get-token(a.$colors, 'form-field->active->border') != 'form-field->active->border' {
     &:focus {
        border: 2px solid a.color('form-field->active->border');
     }
  }
}

but that seems suboptimal (both syntax-wise, and it doesn't handle the possibility that a token path and token value could legitimately be the same). Using sass:map really isn't viable b/c I'd have to repeat the parsing logic.

What do you think?

johncrim commented 4 years ago

Another related option would be to return null when the token doesn't exist. I'd prefer that if possible, but I assume that isn't possible at this point, b/c code that depends on accoutrement probably uses the knowledge that the key path is returned when the key doesn't exist.

james-camilleri commented 4 years ago

Hey @johncrim I actually have an open pull request with the "return null" scenario in place (#41). I'm just wrapping up some unit tests and will finalise all the code, so this might help you out. Sorry it's taken so long, work got extremely out of hand and open source contributions needed to take a backseat.

johncrim commented 4 years ago

@jcamilleri13 : Thank you, that's great! I'm a fan of returning null when keys are missing (and in general of failing fast), thank you for your work on that.

Could you take a look at #49, too? I'm curious if your fix will also address that.