Change the File Extension in Typescript (or Javascript)

Posted by in Software, Tutorial

TL;DR: You need to get the root name (without the extension) and append the new extension. Then, it only depends on how tolerant you are. The other day I figured I needed a function to change the file extension (from a .jpg to a .nfo). Since I haven’t found a ready made function to do this, I decided to create my own (it’s also a good exercise for my neurons…). The function ended up having the following steps: Prerequisite: the function input parameters are the file name (not the full…read more

Consistent Testing

Posted by in Software, Tutorial

TL;DR: Delete the settings file before starting the test. Today I implemented a simple wrapper over the electron-settings functionality to gibe me access to a set of properties via get/set methods. My scenario is simple: I have a property named path.backup and I want to be able to get, set andtest it’s set`. The code is:

Now, my testing code would be something along the lines:

And I was surprised to find the test failed in the second part (can detect it has source paths). Consistently. Then, I…read more

Poor Man’s Mocks in TypeScript

Posted by in Software, Tutorial

Today I went against my better judgement of usign a mock framework and decided to create my own mocks to allow coverage testing for an IPC-calling code. My reference code I wanted to test is:

The code would use an angular service named PathService which, for the purpose of this test only has a method named getSourcePaths(), which returns a Promise. Now, I needed to emulate that method, and the code I wrote to do that is:

It basically defines an object containing a method getSourcePaths() which returns…read more

Use “toThrowError” and “toThrow” in TypeScript with Objects

Posted by in Software

TL;DR: wrap your method call in the expect() statement. Instead of expect(obj.method()).toThrow(), write expect( () => { obj.method() }).toThrow(). While in the process of improving the code coverage, I needed to write a test for a method throwing an exception. My initial code would have been something like:

Where SelectorComponent has a method named updateSelectors(value: any) which throws an error if the value is not in an array. If I write the test as above, it will fail (the test itself will throw the error):

However, If I…read more

Initialize a Typescript Interface with JSON

Posted by in Software

TL;DR: You can initialise a typescript interface using JSON objects. On my quest to load and save objects as JSON, I found that you can convert a JSON object to an interface via something called type assertion. Basically, if you have an interface:

you can load a JSON object via JSON.parse(json) straight to an instantiation of the interface. Then, you can use the said instantiation in e.g. a component. Now, all is nice, but what if you want to initialise an instance of the PathInterface interface? That’s simple. You…read more

Check Duplicated Paths with LoDash (and Angular 2)

Posted by in Uncategorized

One of the things I’m looking at is to be able to create a list of paths I’d want to scan. The first iteration had blind addition to the list. However, this would make the user’s responsibility to manage the duplicated paths, which is not really nice. Therefore, I think it’s a good thing to add some sort of path duplication detection in the code. The pseudo-code would be something like:

In this case, if we have a non-null item, then we already have something in the list. Otherwise,…read more

Angular 2
Refresh Bound Variables from Callback

Posted by in Software

TL;DR: Use ChangeDetectorRef‘s detectChanges() at the end of your callback code. While developing my electron.js app, I’ve had the following workflow: I have an input element and a button The input element is capable of editing a path The button triggers a File Open dialog

The problem I had was the showOpenDialog() accepts a callback and I couldn’t get the input field to update once the user has picked a file. Fortunately, the smart angular developers have thought of this situation too and the solution is quite simple. You…read more

Scan Decorated Classes in Typescript

Posted by in Software, Tutorial

TL;DR: Make the decorator set a property in the class and look for it. While trying my hand at writing an ORM, I had a problem of initialising the database through a dummy instantiation of some models, without running an explicit command. In java, I can use code generation to provide a list of objects following a pattern (e.g. implementing an interface). Here, the concepts are somewhat different and I found out I can use decorators. My model looks like this:

where the @table() and @field are decorators. Ideally,…read more