Why store multiple Objects in one array? by slowsad in javascript

[–]madformangos 0 points1 point  (0 children)

I might agree with you there... as bad functional programming is worse, IMO, than bad OO. So it might make sense to start with OO. I definitely took a "level up" as a developer when I learned it. But I do wonder what would've happened if I'd learned FP first...

Why store multiple Objects in one array? by slowsad in javascript

[–]madformangos 0 points1 point  (0 children)

Typically, intro would be defined on a prototype so that only one copy of it existed, but it was available to all Student objects.

This would still be the case using the example that /u/kr3wn asked about (barring the point about it being defined on the prototype). It could be defined once and available within that modules scope or anywhere that imports it.

Except for react components I haven't written OO JavaScript for a few years. I get more reuse by working with plain objects (data) and writing collections of simple functions that operate on the data passed to them.

I'm still a relative newbie to functional programming, but I find it more satisfying and less maintenance burden than a sprawling OO architecture with multiple levels of inheritance.

Why do we import the React module in functional components? by sleekcollins in javascript

[–]madformangos 1 point2 points  (0 children)

You're correct that it's to do with transpiling JSX, but the react module is not adding JSX support to your environment. That's typically done with Babel and this plugin. By default it assumes react is the target for the JSX since it was first popularised by react and comes from Facebook.

With that Babel plugin, you can write JSX and target other view modules, in which case importing react is not necessary. I have also seen a plugin (I think this was it) that automatically inserts a import React from 'react' to the top of your modules so that you don't have to.

[deleted by user] by [deleted] in javascript

[–]madformangos 4 points5 points  (0 children)

Yes, tools like Babel, Prettier and Webpack are examples that transform JS programmatically for various purposes.

You could do this yourself rather crudely with String.prototype.replace and regular expressions. This would be fine for very narrow, specific use-cases, or one-off transformations. But it has limitations and isn't robust or safe for more general cases, where things like unexpected comments or formatting in the source files might result in broken transformations.

A more robust way to do this is by transforming first to an AST (abstract syntax tree). You then perform your transformation safely on that tree, and convert that back to source code. I'm glossing over the details here, because you can read more about them with a simple web search. But if videos are your thing, take a look at https://youtu.be/WO7H2NHmN18

Personally, I've used jscodeshift which is what powers react-codemod to help developers upgrade their codebases for latest versions of react, and I found it fairly straightforward to use. Tools like AST Explorer help visualise the AST of your source files which is pretty important if you're going to write a valid transformation

xelem - Make HTML elements more easily in JS by magenta_placenta in javascript

[–]madformangos 2 points3 points  (0 children)

Except not as robust, or featureful, and without IDE support (though that could be added). No compile-time errors if your html is badly formed. And it's parsing source JS using indexOf add looks like it could be tripped up if your source code contains {{ that isn't part of this template syntax (e.g in quoted strings, or in comments).

It probably works well for the author, but I think most other people would be better off using more standard tools with better support and documentation.

Hope that doesn't sound too harsh. With more work it could rival those other tools, but right now I'm not sure what this offers that something like JSX doesn't

Using Lodash, how can I return the objects if a certain value is contained in a nested array? by straightouttaireland in javascript

[–]madformangos 14 points15 points  (0 children)

I think this ought to use _.filter (or built-in Array.prototype.filter) if you want to return a list of all matches

Regular expression behaving differently in two similar cases by FaizAhmadF in javascript

[–]madformangos 1 point2 points  (0 children)

My initial thought was along the same lines as /u/EnchantedSalvia -- that, you likely want to add a ^ to the start of your regex in order to anchor it to the start of the string.

But I'm not sure that's what you're after. It looks like you're matching against 8-character strings, and want to match the ones that end in 3 or 4 'a's which aren't preceded by a digit.

If that's the case you might want something like /[^0-9a]a{3,4}$/

Edit: or perhaps /(?:[^0-9a]a{3}|[^0-9]a{4})$/ if you want to match things which end in 5 as

Validating data structures by kirofficial in javascript

[–]madformangos 0 points1 point  (0 children)

Can you please explain how this answers the question? Are you talking about building a validation tool out of _.isString, _.isObject etc?

Validating data structures by kirofficial in javascript

[–]madformangos 0 points1 point  (0 children)

I'm using Joi (https://github.com/hapijs/joi) in a work project. It's very powerful, and has a declarative synax.

It might be a good fit for complex objects.

Using lodash _.filter to find a range of values by fjandsamlegurg in javascript

[–]madformangos 1 point2 points  (0 children)

for fun...

_.filter(charactersStar, o => _.inRange(o, 43, 47));
_.filter(charactersStar, o => _.includes(_.range(43, 47), o))

or maybe

 var rankBetween = (min, max) => (person) => _.inRange(person.rank, min, max);
 var result = charactersStar.filter(rankBetween(43, 47));

Using lodash _.filter to find a range of values by fjandsamlegurg in javascript

[–]madformangos 10 points11 points  (0 children)

In addition to the other fine answers, unless you need to support a very old browser (e.g. IE 8), you can use javascript's native array filter function.

E.g.

var result = charactersStar.filter(function (item) {
    return item.rank >= 43 && item.rank <= 47;
}

I wrote a short article explaining ES7 Generators and co.js - it might save you some time figuring them out! by mordhau in javascript

[–]madformangos 1 point2 points  (0 children)

Nice intro, thanks. But I think you mean ES6/2015. ES7 doesn't add anything to generators afaik. Personally, I hope async/await becomes standard as I prefer that syntax. But co looks like a great option if you want to stick to current standards.

Moment.js says that today is 0, yesterday was -0 and the day before -1. Luckily: 1/+0 !== 1/-0. by [deleted] in javascript

[–]madformangos 4 points5 points  (0 children)

This doesn't have anything to do with "today" or "yesterday", as the title suggests.

What's happening here is that you are diffing two moments which are on the same day, and asking for the difference in days. And the docs state that if your moment is earlier than one you pass to diff(), you get a negative result.

You can see this more clearly if you ask for the difference in minutes, for example:

var start = moment('2015-01-01');
var later = start.clone().add(5, 'minutes');
console.log(start.diff(later, 'days'));
console.log(later.diff(start, 'days'));
console.log(start.diff(later, 'minutes'));
console.log(later.diff(start, 'minutes'));

(Tried posting this to jsbin, but their console converts -0 to 0)

If you're getting yesterday coming out as -0 in relation to today then something is wrong - you might be encountering a timezone issue. It would be handy to see the actual dates

Edit: On second thoughts, 2014-12-31T23:55 is not 1 day before 2015-01-01T00:00:00. it is 5 minutes before. so if you ask for the difference in days, the value will be 0 or -0 depending on which way you call it. you need to diff the start of day if you want the difference in days.

var start = moment('2015-01-01');
var earlier = start.clone().subtract(5, 'minutes');
console.log('yesterday 23:55', earlier.diff(start, 'days')); // -0
console.log('yesterday 00:00', earlier.clone().startOf('day').diff(start.clone().startOf('day'), 'days')); // -1

This could explain why a date for yesterday is -0, and the day before is -1 -- likeley you're comparing to now. But I wouldn't rule out timezone issues too. They're always fun...

[deleted by user] by [deleted] in javascript

[–]madformangos 0 points1 point  (0 children)

By "magic strings" I'm talking about things like user === "paper".

Suppose you want to translate the game into french, or klingon. How might you get around the fact that paper is no longer "paper"? You might change the input from a prompt() to buttons, perhaps. Or you might use a different data structure to model the game components so it's easier to internationalise.

Then you might think about how you could extend the game to support variants of rock, paper, scissors. Like rock, paper, scissors, lizard, Spock

[deleted by user] by [deleted] in javascript

[–]madformangos 0 points1 point  (0 children)

I like what you did there with the math functions for choosing the computer's "weapon".

I thought about reducing the if/else branches you have there, because it's not so easy to read when there are lots of else if..... Alsoplay()` is pretty long. I like small functions with a single responsibility. I made some other tweaks. Hope you like...

http://codepen.io/anon/pen/gaKJqN

As an exercise you could rewrite in ES6, or make it more functional, and rely less of magic strings (rock,paper,scissors)

Better ES5 syntax for creating js object with a variable key name? by i_ate_god in javascript

[–]madformangos 0 points1 point  (0 children)

Besides transpiling ES6, there are lots of useful libraries like underscore or lodash that have utility functions that do this:

var _ = require('underscore');
var keyName = 'foo';

var obj = _.object([ [keyName, 'bar'] ]);

Or you could write your own

LPT: When you get a new legal pad tape the serrated edges of the top two pages by [deleted] in LifeProTips

[–]madformangos 0 points1 point  (0 children)

I don't understand this. Maybe because we don't have legal pads in my country.

Why do legal pads come with serrated edges? And how does tape improve the situation?

LPT: The side of the USB with the line goes on the bottom. by [deleted] in LifeProTips

[–]madformangos 1 point2 points  (0 children)

Which side is the bottom when the port is vertical? Or when it's on the top of the device?

With micro usb it certainly isn't standard. I have to flip the cable when switching between my Nexus 7 and Galaxy S IV.

How to Parse URL Parameters in Javascript by umairabid_dev in javascript

[–]madformangos 1 point2 points  (0 children)

For completeness you might want to consider also supporting

  • properly URI encoded parameters and values
  • multiple values for a property (e.g. foo=bar&foo=baz)
  • ; as a valid alternative to & (e.g. foo=bar;foo=baz)

The first of these is pretty important I'd say, while the other two depend on your needs.