Thursday, January 7, 2016

Underscore.js(_) : Difference between _.where() and _.findwhere()


Underscore.js is a utility-belt JavaScript library for JavaScript that provides a lot of the functional programming. You can easily manipulate data collection to sort and display it.

_.where ():  

where() function always return the object or list of object contains the specific property from collection of object.

Syntax:
_.where(list, filters);

·         list – collection of item
·         filters – parameters which contains conditions

Here is a collection of objects:

var goal = [
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 2,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "1"
    },
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "2"
    },
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 2,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "3"
    },
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "4"
    }
]


var filteredGoal = _.where(goal, { achievability: "3" });

filteredGoal is having all matching objects, which have achievability property value 3.

[
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "2"
    },
    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "4"
    }
]


_.findwhere() : findwhere () function always returns the first matched object in collection.

Syntax:

_. findwhere (list, filters);

·         list – collection of item
·         filters – parameters which contains conditions

var filteredGoal = _.findwhere(goal, { achievability: "3" });

filteredGoal is having only first matching record of goal collections

    {
        "category": "education",
        "title": "Charlie University",
        "description": "Lorem ipsum dolor sit amet",
        "date": "01/03/2020",
        "value": 50000,
        "achievability": 3,
        "experimental_achievability": 3,
        "suggested": false,
        "accounts": [],
        "articles": [],
        "related_goals": [],
        "id": "2"
    }

Additional link: 

Tuesday, January 5, 2016

Installation and Configuration of Karma [Jasmine Unit test runner]

Karma Installation

There are few steps to install Karma ( Jasmine Unit Test Runner )

1.       Download and Install Node.js


2.       Download and Install Python 2.7

3.       Set Python Environment Variable
[npm Config set python python 2.7]

4.       Run command to install Karma
 [npm install –g Karma]  

5.       Configure Karma js file [ Karma excute unit tests according to a karma.config.js configuration file ] you can generate automatically  karma.config.js  file by running following command
[karma init]

6.       Start Karma
Karma start karma.config.js 


Sample js file of karma.config.js

module.exports = function(config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath: '',


        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks: ['jasmine'],


        // list of files / patterns to load in the browser
        files: [
           'Scripts/jasmine/jasmine.js',
           'Scripts/angular.js',
           'Scripts/angular-mocks.js',
           'Scripts/angular-route.min.js',
           'Scripts/angular-ui-router.min.js',
           'Scripts/angular-*.js',
           'Scripts/ui*.js',
           'app/app.js', // do not change app.js position
           'app/Area/controllers/summaryController.js',
           'Scripts/Test/*Spec.js' // Jasmine Test
           ],


        // list of files to exclude
        exclude: [],


        // preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        preprocessors: {
   
        },


        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters: ['progress'],
        //reporters: ['spec'],
        //specReporter: {
        //    maxLogLines: 5,         // limit number of lines logged per test
        //    suppressErrorSummary: false,  // do not print error summary
        //    suppressFailed: false,  // do not print information about failed tests
        //    suppressPassed: false,  // do not print information about passed tests
        //    suppressSkipped: true  // do not print information about skipped tests
        //},
        plugins: ['karma-ie-launcher', 'karma-chrome-launcher', 'karma-jasmine'],

        // web server port
        port: 9876,


        // enable / disable colors in the output (reporters and logs)
        colors: true,


        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,


        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: true,


        // start these browsers
        // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
        browsers: ['IE','Chrome'],


        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun: false,

        // Concurrency level
        // how many browser should be started simultanous
        concurrency: Infinity
    });
}

Additional Source Links :
https://www.jetbrains.com/pycharm/help/running-unit-tests-on-karma.html
http://www.methodsandtools.com/tools/karma.php



Friday, January 1, 2016

Angularjs : foreach loop

angular.forEach() invokes the iterator function once for each item in obj collection, which can be either an object or array.

Syntax of ForEach loop in AngularJS:
angular.forEach($scope.data, function(value, key){});

Here are few example of angular.forEach() function:
1. Object Properties iteration:
   angular.forEach() function can be used to iterate of each property of object.

var values = {name: 'misko', gender: 'male'};
angular.forEach(values, function(value, key)
 {
    console.log(key + ': ' + value);
});

2. Object Collection Iteration:
angular.forEach() function can be used to iterate of each item of collection.

var values = [
  { "Name" : "Thomas", "Password" : "thomasTheKing" },
  { "Name" : "Linda", "Password" : "lindatheQueen" }
]

angular.forEach(values, function(value, key)
 {
  console.log ('Index:' + value.Nam);
  console.log ('Name: ' + value.Name);
  console.log ('Password: ' + value.Password);
});

Here key will work as Index of object in collection

Other blogs related to AngularJs :

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...