Showing posts with label solution. Show all posts
Showing posts with label solution. Show all posts
Tuesday, November 26, 2019

ES6 Recipe: JSON.stringify Map

The keys of a Map can be anything, including objects. But JSON syntax only allows strings as keys...

Let's stringify Map instance to JSON...
Tuesday, June 5, 2018

TypeScript Recipe: Elegant Parse Boolean

There are many ways to convert String to Boolean or Number to Boolean, etc.
Here we will do it all in one and stop worry about form of boolean like parameters we receive from outside.


Lets' Convert 1 '1' and 'true' to true and 0 '0' 'false' null and undefined to false
Thursday, April 5, 2018

React Recipe: Components Error Handling

This article is about the Errors Handling in React Components using TypeScript

Not handled error in any part of the UI could crash the whole application.
Error Boundaries only catch errors in the components below them in the tree.
As of React 16, errors that were not caught by any error boundary will result in unmounting of the whole React component tree.
Catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI.
Use JS error reporting services, so that you can learn about unhandled exceptions as they happen in production, and fix them.
Thursday, December 7, 2017

Chasing the Trunk Based Development

A source-control branching model, where developers collaborate on code in a single branch called trunk, resist any pressure to create other long-lived development branches by employing documented techniques. They therefore avoid merge hell, do not break the build, and live happily ever after..
Release branch is typically a snapshot from trunk with an optional number of cherry picks that are developed on trunk and then pulled into the branch.
Thursday, June 29, 2017

JavaScript Recipe: Round to a number of decimal places stripping trailing zeros

JS uses binary floating point to handle decimal based operations.
JavaScript calculations with floating point causes the Nonsense cases especially during the result comparisons & displaying.
If the variable was created as a Number, not a String, extra trailing zeros would be dropped automatically.
Friday, March 31, 2017

Angular recipe: Get State updates from Redux

This article is about the possible ways to retrieve your data State in Angular (read 2 or 4) from Redux Store.


It's assumed that you are already familiar with Redux ecosystem. If not click an image below:

Monday, January 30, 2017

Java recipe: Initialize an ArrayList with zeros

It's often useful to have a list filled with zeros of the specified length N.
Why would you want one? Well, it's handy during the work with algorithms, or just to avoid null checks.
Usually, instead of the List filled with 0s an Array is enough: new int[N]
- creates an array of N integers with zero value.
new ArrayList(N) (has the size 0) is NOT the same as new int[N] (has the length N)

- The integer passed to the constructor of list represents its initial capacity (the number of elements it can hold before it needs to resize its internal array) which has nothing to do with the initial number of elements in it.
Whatever, below are the bunch of ways to create the List filled with zeros, if you need one.
In each example we will create a list filled with 100 zeros. (Java8 implementation with StreamAPI included).
Friday, July 15, 2016

Java recipe: Find duplicates in your List

Examples to count and receive duplicate objects in a list.
Wednesday, April 27, 2016

Regex Recipe: Normalize URL

This recipe describes how to replace multiple slashes in URL & avoid replacing the first // in http:// & https://.

Expected behaviour

To send some concatenated url with multiple slashes and recive the clean one.

Given Input:
http://devcdn.some-hub.net/html5//280131/publish///brand/bundle/games/
Expected Output:
http://devcdn.some-hub.net/html5/280131/publish/brand/bundle/games/
Monday, April 11, 2016

Java Recipe: Read resource file as a String.

This recipe shows how to load the file from the resource folder and receive it's content as a String.

Project structure

|--src
|  \--main
|       \--java
|       \--resources 
|          |--lines.json

Get file path from resources folder

In order to receive the resource with given name we need to use java.lang.ClassLoader:
Friday, March 11, 2016

All your Calendars in one Place

On which side are you?

Obviously an entry point is either IOS or Android, but the rest mostly doesn't meter.

Google Calendar (Android)

The most awesome thing, that you're able to display calendar from any third-party service, that provides ICalendar feed (like a Trello, in your Google Calendar)
In addition Google Calendar automatically fetches and adds the Events from Gmail - flights, hotel, concert, restaurant reservations, bookings (e.g. from Booking.com or Airbnb)

Flexibits Fantastical (IOS)

You could sync Fantastical with your Google Calendar as well, so basically you'll have all benefits from google as well.
Tuesday, March 8, 2016

Angular Recipe: Avoid empty option in HTML select element

HTML select in Angular JS


HTML select element is often used to create a drop-down list.
You could use either ng-repeat or ng-options directive to fulfill it with options.
Note that the value of a select directive used without ngOptions is always a string.
That's why i prefer to use ng-options directive rather than ng-repeat one.
Note: if ng-model value is undefined, has a wrong type or is not contained among the options set, than selected element into your drop-down would be empty.
Note: It also might happen when ng-model variable is distinct into the child-scope from the one that u've defined in JS.
If you are working with objects that have an identifier property, you should track by the identifier instead of the whole object.

Should you reload your data later, ngRepeat will not have to rebuild the DOM elements for items it has already rendered, even if the JavaScript objects in the collection have been substituted for new ones. For large collections, this significantly improves rendering performance. If you don't have a unique identifier, track by $index can also provide a performance boost.
According to (Prototypical inheritance) your directive might create a child-scope, which could distinct your ng-model="fooBar" variable and no longer refer up to the parent’s $scope.fooBar variable.

To not storing the data directly on the scope helps in this case.
Monday, February 1, 2016

Java: Convert JSON to DTO Object

Use Jakson

U can convert JSON to DTO and back automatically with ObjectMapper:
  • Convert Java object to JSON, writeValue(...)
  • Convert JSON to Java object, readValue(...)
  • Convert JSON to List of Java objects, readValues(...)
U can use field with different name with @JsonProperty(...) annotation.
U can skip unneeded properties with the following method: configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Friday, January 29, 2016

Check HTTP Response Status for Success

All HTTP 2xx mean successful status codes.
So, it's fairly easy to check, but a bit harder to not duplicate those checks everywhere:)
Monday, January 25, 2016

MySQL: varchar foreign key pitfalls

Why VACHAR as Foreign Key might be needed

Imagine that you have two tables BOOKS & GENRES.
You need to have genre column into your BOOKS which would be one from your GENRES

Depending of which data either genre_id or genre_name u'll keep into your new column, your BOOKS table may look like one of the following:

* Genre Id as FK
id *(pk) title author genre_id *(fk)
1 The Little Prince Saint-Exupéry 1
2 Fear and Loathing in Las Vegas Hunter Thompson 2
Or:
* Genre Type as FK
id *(pk) title author genre_type *(fk)
1 The Little Prince Saint-Exupéry FICTION
2 Fear and Loathing in Las Vegas Hunter Thompson ALTERNATIVE
The second table is obviously more readable & understandable.
Tuesday, January 12, 2016

Git Tips & Tricks: Case-sensitive Only filename changes

Sunday, November 8, 2015

Best Practice: Anonymous Closures

Manage Access & Namespaces via Closures

Anonymous Closures allow to maintain modularity and to prevent exposing the private modules data.
Global variables have unclear scope leading to tough manageable apps. Invocation of the global variables is expensive, because it checks entire scope depth.
The objects with the same names might be overridden if those are not within the separate namespaces.
Here are few points how to start doing better:
  • use namespaces to prevent conflicts & unexpected overrides;
  • use anonymous closures to encapsulate & isolate your app private data;
  • return the result object with the only api that you want to expose;
  • avoid direct global variables usage, use imports istead;
  • group file content around needed data.
Friday, October 23, 2015

Git Tips & Tricks: Make the current commit the only (initial) commit

Here is the brute-force approach, which also removes the configuration of the repository.

Keynote: Regexp Short Notes

Regular expressions

Regular expression is a pattern describing a certain amount of text. So, Regexps are quite useful whatever programming language u're using.
Friday, October 16, 2015

Angular Recipe: Passing data into your Directive

Angular JS


It's often needed to pass some data into your directive from outside.
By default, directives are restricted to attribute 'A' type and are therefore restrict: 'A' is redundant.
Inside directives, it is best practice to use controller only when you want to share functions with other directives. All other times you should use link.
If a directive is only returning the link function you can write it as just return anonymous function: return function(scope, element, attr) {/*...*/};