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.

[Suggestion] Concat nested objects (BEM)

See original GitHub issue

Hi,

classnames is a ubiquitous, indispensable library for creating classnames and I like it a lot.

However, it could be improved for optimizing creating series of BEM classes that have similar modifiers.

For example, let’s say I have the following code:

foo = {
 'cool-block': true,
 'cool-block--success': successful,
 'cool-block--error': error,
 'cool-block--pending': pending,
}

Not so bad, there’s a little code repeating but it’s bearable at least. However, let’s say I had some other element in the same file that depended on the same success/error/pending:

bar = {
 'other-block': true,
 'other-block--success': successful,
 'other-block--error': error,
 'other-block--pending': pending,
}

Again, on its own it’s bearable but together there’s an opportunity for reducing repeating code. It could be defined similar to the way it would in scss:

mods = {
 '--success': successful,
 '--error': error,
 '--pending': pending,
}
foo = {
 'cool-block': mods,
}
bar = {
 'other-block': mods,
}

Currently, if you pass a non-empty object it will evaluate as true, and an empty object evaluates as false. This means that, even if dynamically generating your nested objects, existing behavior is preserved (aka backwards compatible).

Thoughts?

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
newyork-anthonyngcommented, Apr 12, 2017

@thaithco Good idea, but I’m wondering about the performance consequences this would have.

I agree with you that this package is ubiquitous, but I’m not sure that BEM is as commonly used.

I think that it should be the responsibility of the developer to create a BEM object that works with the classnames package.

Would a utility function like the below work?

const createBemObject = ({ baseNames, modifier }) => {
  const result = {};
  for (var i = 0; i < baseNames.length; i++) {
    for (var key in modifier) {
      const newKey = baseNames[i] + key;
      result[newKey] = modifier[key];
    }
  }
  return result;
};

const success = true;
const error = false;
const pending = true;

createBemObject({
  baseNames: ['cool-block', 'other-block'],
  modifier: {
    '--success': success,
    '--error': error,
    '--pending': pending,
  },
});
/*
{
  'cool-block--success': true,
  'cool-block--error': false,
  'cool-block--pending': true,
  'other-block--success': true,
  'other-block--error': false,
  'other-block--pending': true,
}
*/

1reaction
jednanocommented, Jul 20, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

C# Nested object and concatenation - linq - Stack Overflow
In short, you're trying to create a nested structure from flat data - the opposite of what the class names suggest.
Read more >
How to Merge Deeply Nested Objects in JavaScript
There are many scenarios where we need to merge two objects that may be deeply nested. In this article, we will see how...
Read more >
Nested field type | Elasticsearch Guide [8.5] | Elastic
The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way...
Read more >
merge-anything - npm
Merge objects & other types recursively. ... merge-anything will merge objects and nested properties, but only as long as they're "plain ...
Read more >
JavaScript Merge Objects - Scaler Topics
This article by Scaler Topics will explain various ways and cases in which we can merge two JavaScript objects.
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