BootDB

Code | Open Source | Web

Fix Floating Point Precision in JavaScript

toPrecision is a JavaScript built-in function which can be used to set precision of a floating number, example usage:

var number = 123.456789;
number.toPrecision(5); // returns string: '123.46'

To convert string to number use:

Number(number.toPrecision(5));

 

Search and Replace Commands in Linux

Sed command does a great job for search and replace, let’s see an example:

sed -i -- 's/foo/bar/g' myfile.txt

This will convert the string “foo” to “bar” in myfile.txt, if you would like to perform this on all files use:

sed -i -- 's/foo/bar/g' *

The same can be done recursively using find command:

find . -type f -exec sed -i 's/foo/bar/g' {} +

How to Resize a Image in Linux

Linux’s convert command line tool can be used to convert between image formats as well as resize an image, let’s see how to resize an image:

convert my_picture.png -resize 200x100 my_picture_200x100.jpg

Sprintf Equivalent in Javascript

Below are the two methods by which we can emulate sprintf in Javascript directly to console.log and String.format:

console.log approach:

(function() {

    var logger = console.log;

    console.log = function() {

        var format = function(str, args) {
            logger(args);
            return str.replace(/{(\d+)}/g, function(match, number) {
                return typeof args[number] != 'undefined' ? args[number] : match;
            });
        };
        var inputs = Array.prototype.slice.call(arguments);
        logger(format(inputs.shift(), inputs));
    }

})()

console.log("Hello {1}", 10, 100);</pre>
<p></p>


String.format approach:


    if (!String.prototype.format) {
      String.prototype.format = function() {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function(match, number) { 
          return typeof args[number] != 'undefined'
            ? args[number]
            : match
          ;
        });
      };
    }
    "{0} is dead, but {1} is alive! {0} {2}".format("ASP", "ASP.NET");

How to Trigger Event on Pressing Enter Key in AngularJS

Use the below directive which triggers a function when enter / return key is pressed:

.directive('ngEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if (event.which === 13) {
                scope.$apply(function () {
                    scope.$eval(attrs.ngEnter);
                });
                event.preventDefault();
            }
        });
    };
});

Usage:

<input ng-model="input" ng-enter="enterTriggered">

How Emit a Event When Ng-repeat Is Completed AngularJS

Use a directive with ng-repeat which triggers a function when ng-repeat reaches to last element, directive code:

.directive('onFinishEmit', function($timeout) {
    return {
        restrict: 'A',       
        link: function(scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function() {                        
                    scope.$emit(attr.onFinishEmit);
                });
            }
        }
    }
});

Usage:

<div ng-repeat="data in dataset" on-finish-emit="renderComplete">

</div>