Get all index values of a RegExp with the exec RegExp prototype method in javaScript

The exec method of the RegExp class in javaScript is what I have come to find is useful for getting an array of index values for each instance of a pattern that can be found in a string. There might be a number of other ways to go about doing this, however the use of exec might be the best way to go about doing so rather than working out a solution with the string match method.

The RegExp exec method will execute a search for the pattern that it is call off of for the given string, and will return a result array if a match is found, or null if no match is found. In addition regular expression Object instances have a last index property that will be updated each time the exec method is called. So the exec method can be used with a loop of some kind along with the global flag to get all index values of the pattern in a string that is passed as the first argument when calling exec.

So because each time the exec method is called an object with the index value of the beginning of a pattern match is obtained, the exec method is great for creating an array of Objects with detailed information with each pattern match. So this makes the exec method a great solution for projects that are parsers, or any kind of project where an array of detailed pattern matching data is needed.

Read More

Git diff examples

The git diff command is useful for finding changes between two commits when using git for source control. there are many options for formatting the output also, so for one example say I am just interesting in getting a list of files that have changed from a given starting and ending commit it, such a task can be completed by using the git diff command with the name only option.

I am also a javaScript developer so this post will also be on a few quick examples that make use of the child process module in core nodejs to make git command calls that help get a list of files that have changed between tow commits in a repository. However I will be going over some examples that can just be used in the command line interface also of course.

Read More

String charAt, the bracket syntax, and char code at in javaScript

In javaScript there is the charAt string prototype method that can be used as a way to get a single character in a javaScrit string, there is also just using the bracket syntax as a way to get a single char also though. I can not say that I use the charAt method often as the same effect can be achieved using the bracket syntax, the same way that one would get an element in an array, or any public named key value in any javaScript object for that matter not just the wrapper object of a string primitive. So the bracket syntax strikes be as a better way to go about getting a single char in a string, as it is more versatile.

There is also the char code at method that is also in the javaScript string prototype object that does more or less the same thing as charAt only with one important difference. The charCodeAt method will give a number value that is a UTF-16 code unit for the char rather than a string of the single char.

In this post I will be covering ways to go about getting a single char from a string, but also hopefully break ground into other ways of going about getting at what one might be after when it comes to getting something in a string, number, or any object.

Read More

Canvas Example grass blades

I just have to make another canvas example post now and then, life is short and I have to do something that I really want to do with my life now and then just like everyone else. So for today I made a grass blades thing with canvas and a little bit of javaScript code. I can not say that this is my dream project mind you, but I think it might be worth it for me to expand my collection of this kind of content when it comes to fun, and artful examples of javaScript and canvas in action rather than just the boring yet practical stuff.

This canvas example makes use of a blade javaScript module that is used to create a single blade of grass, and then there is another grass module that serves as a way to create a collection of blade objects. These two modules are just yet another pattern that seems to come up with most canvas projects, having some kind of standard for a display object of some kind, and another for a collection of these kinds of display objects.

I then as always with these canvas examples have a draw.js module that is used to render the state of one of these grass objects to a canvas element. This draw module is where I am actually drawing to a canvas element, but it is nt where I am creating an injecting it. This way I am separating things between the javaScript code that creates a main state object, and code that renders that aspects of that state object to the canvas, and then code that creates a state, uses the draw module, and ties everything together.

With what is going on in the outside would as of late I have come to find that I like to work on canvas projects as a way to gain an escape of sorts from what is going on. JavaScript and canvas have always been there for me as a way to get away from my troubles that may prove to be a more constructive alternative to that which is often typical. So that is the kind of canvas example this is, just letting go of what is bothering me and letting my mind flow. The result is another canvas animation type thing that I can have running on the screen that is nice to look at for a while, and I can just thing about all the other things I can do to make it more interesting.

Read More

lodash reject compaired to lodash filter and other native javaScript options

I often start out each new month with a new post on lodash because it would seem that there is still much interest in this utility library despite its age. For this post I will be working out a few quick examples of the lodash reject method that is just simple an inversion of the lodash filter method. This method in lodash is not really one of the redeeming methods in lodash that make the library worth while as there is of course the native array filter method that can be used to quickly create a reject method in plain old javaScript by itself.

Still it would seem that there are some talking points with this one as the lodash reject method is one of the collection methods in lodash that will work with both arrays and many objects in general. Still it is not so hard to just work out ways of doing the same thing with just native javaScript by itself. So this will of course not just be a post on the lodash reject method but also native javaScript examples that do the same things the lodash reject method does.

Read More