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.

Passing arrays to cx results in a className string with commas

See original GitHub issue

sample code here: https://github.com/brianbegy/classnames-array-poc

const cx = require("classnames");

function returnsAnArray() {
  return ["arrays", "have", "commas"];
}

console.log(cx("strings", { ["are"]: true }, { ["ok"]: true }));
console.log(cx("but", { [returnsAnArray()]: true }));

expected result: strings are ok but arrays have commas

actual result: strings are ok but arrays,have,commas

Is this the intended behavior?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
newyork-anthonyngcommented, Oct 8, 2019

@brianbegy the default behavior of JavaScript is:

  • if an array is an Object’s key
  • convert each array element into a string
  • and join them together with commas
function returnsAnArray() {
  return ["arrays", "have", "commas",{}];
}

const myObject = { [returnsAnArray()]: true }
console.log(Object.keys(myObject)); // ['arrays,have,commas,[object Object]']

The commas seem to be something related to JavaScript, and not this package.

1reaction
brianbegycommented, Sep 26, 2019

Having looked through the code and written a solution, it is pretty obvious that “fixing” it would have a serious performance cost.

I’ll leave this open for the moment, but this looks like a “won’t fix” to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add multiple classes to a ReactJS Component?
I use classnames when there is a fair amount of logic required for deciding the classes to (not) use. An overly simple example:...
Read more >
JavaScript Tutorial: The Basics
split(delimiter): returns an array by splitting the string using delimiter . Searching/Modifying Strings using Regular Expression: See "Regular ...
Read more >
D3 Basics - D3 - A Beginner's Guide to Using D3
data() is called it iterates over the array and passes each value down the method chain. 5 gets joined with the first <circle>...
Read more >
5. Working with Arrays and Loops - JavaScript Cookbook [Book]
For instance, if you create an array literal, you can use commas to delimit array elements that don’t yet exist: var arrLiteral =...
Read more >
fitcecoc - Classification - MathWorks
The cross-validation results determine how well the model generalizes. ... By default, when you pass tall arrays to fitcecoc , the software trains...
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