camelCase to kebab-case

I have a one-liner similar to Marc’s but with a simpler Regular Expression and ~20% faster according my benchmark (Chrome 89). const kebabize = (str) => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? “-” : “”) + $.toLowerCase()) const words = [‘StackOverflow’, ‘camelCase’, ‘alllowercase’, ‘ALLCAPITALLETTERS’, ‘CustomXMLParser’, ‘APIFinder’, ‘JSONResponseData’, ‘Person20Address’, ‘UserAPI20Endpoint’]; console.log(words.map(kebabize)); [A-Z]+(?![a-z]) matches any consecutive capital … Read more

Exporting a class with ES6 (Babel)

Browserify is meant to be fed a single “entry point” file, through which it recursively traverses all of your require statements, importing the code from other modules. So you should be require‘ing the _babel.js versions of modules, not _browserified.js ones. From the looks of it, you intend for your app’s “entry point” to be demos/helicopter_game/PlayState_browserified.js, … Read more

Why do both Promise’s then & catch callbacks get called?

The then callback gets called because the catch callback is before it, not after. The rejection has already been handled by catch. If you change the the order (i.e. (promise.then(…).catch(…))), the then callback won’t be executed. MDN says that the .catch() method “returns a new promise resolving to the return value of the callback”. Your … Read more

Terse way to intersperse element between all elements in JavaScript array?

Using a generator: function *intersperse(a, delim) { let first = true; for (const x of a) { if (!first) yield delim; first = false; yield x; } } console.log([…intersperse(array, ‘&’)]); Thanks to @Bergi for pointing out the useful generalization that the input could be any iterable. If you don’t like using generators, then [].concat(…a.map(e => … Read more

Do I need to import React for stateless functional components?

Yes, there is. Babel transpiles JSX to use React: <div></div> To: React.createElement(“div”, null); So your JSX is internally transpiled to use React.createElement in pure JavaScript, which does use React. Remember that JSX is just syntactic sugar over pure JavaScript. If you don’t specifically import it, it will report that React isn’t defined. Update Sep 2021: … Read more

Can I run a JS script from another using `fetch`?

Fetch API is supposed to provide promise-based API to fetch remote data. Loading random remote script is not AJAX – even if jQuery.ajax is capable of that. It won’t be handled by Fetch API. Script can be appended dynamically and wrapped with a promise: const scriptPromise = new Promise((resolve, reject) => { const script = … Read more

How to add mixins to ES6 javascript classes?

Javascript’s object/property system is much more dynamic than most languages, so it’s very easy to add functionality to an object. As functions are first-class objects, they can be added to an object in exactly the same way. Object.assign is the way to add the properties of one object to another object. (Its behaviour is in … Read more

techhipbettruvabetnorabahisbahis forumueduedusedusedueduseduseduseduseduedus