lodash split and String.split for splitting a string into substrings

So now and then when making a project with javaScript, there might be a need to split a string into an array of strings by a given separator pattern in the form of a string or a regular expression. In lodash there is the _.split method, and there is also a native javaScript split method as well in the String prototype object that can be used to do break a string down into an array of sub strings.

This is not a method in lodash that does a good job of supporting a case to use the full lodash library these days as the native array split method is well supported, and the lodash split method does not really bring anything more to the table compared to other lodash methods where that is the case at least.

Use of a split method in lodash or vanilla javaScript comes up a lot when researching javaScript code examples for various things, so if you are new to javaScript this is one of many methods that should be well understood.

Read More

The lodash partial method for creating abstractions for complex functions

The lodash _.partial method in lodash can be used to create a new function from another function and some starting arguments. In other words it can be used to create a simplified function that only accepts a few arguments that will be used with some set static values when using another method that accepts more arguments. Simply put it is a way to create an abstraction, or an alternative interface for a complex method that accepts many arguments.

There are a number of ways to go about creating simplified methods that call other methods that are a little more complex and take many arguments. One way of doing so would be to just write a new function that calls the other function with certain arguments set to whatever static values that are to be used, or parse whatever values need to be pares for from what was given. Yet another way to deal with abstractions is to just not use them at all to begin with actually, and just work with functions that take many arguments just design the argument in such a way so that they take an options object rather than a set of arguments and have it so the function will just go with certain hard coded default values.

If you are still confused maybe it would be best to just look at some code examples not just with lodash, but also when it comes to just monkeying around with native javaScript by itself actually. So lets take a look at _.partial in lodash, as well as some plain vanilla javaScript code that does things similar to what the lodash partial method is all about.

Read More

Setting a Circle for collision detection in phaser 2

In this post on phaser 2 I will be writing about using the body.setCircle method to make the collision detection work with a circle shape rather than the default square shape. For many phaser 2 powered games the default square shape works just fine, but for other games that involve ball shaped display objects it is necessary to set the physics body shape to that of a circle. The reason being that by doing so when a ball like object hits another at an angle it will bounce as expected of a ball shaped object rather than a square.

Read More

Booleans in javaScript what to know

In javaScript one of the most important primitive values to work with is a js boolean value that will store a true of false value. To create a boolean there is the boolean literal, and the Boolean object that can be used as a javaScript constructor function to create a boolean also. In addition booleans are often what is returned when using a method such as with the lodash isArray method, and can happen as a result of an evaluation of an expression also.

A boolean is a value that only has two possible values true, or false, and as such numbers, and other values can often be used as a replacement for boolean values. Although doing so will eat up more memory, and often a boolean is just the appropriate choice for simple true and false values when it comes to code readability. There are some tricks that I have picked up here and there when it comes to booleans, so I will be sure to write about everything that I know about in this post when it comes to Booleans and how to work with them is a javaScript programing environment. And also while I am at it I will be branching off into some other closely related topics when it comes to javaScript programing in general as usual.

Read More