Skip to content

Commit

Permalink
Added test runner + jshint, added LoDash available through AngularJS …
Browse files Browse the repository at this point in the history
…DI service. Added 'has' in to filter list, because it was missing. Updated examples.
  • Loading branch information
RopoMen committed Aug 11, 2015
1 parent 8c579f4 commit b78a42b
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 59 deletions.
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ angular.module('app', ['ropooy-angular-lodash']);
##### or just a some parts
* 'ropooy-angular-lodash' (all)
* 'ropooy-angular-lodash/utils' (API only)
* 'ropooy-angular-lodash/service' (LoDash as AngularJS DI service)
* 'ropooy-angular-lodash/filters' (Filters only)
* 'ropooy-angular-lodash/filters/groupBy' (Just a specific filters)

Expand All @@ -24,15 +25,22 @@ Default stuff for these examples.
```html
<script type="text/javascript">
angular.module('myApp', ['ropooy-angular-lodash']);
app.controller('MyCtrl', function($scope) {
app.controller('MyCtrl', function($scope, _) {
$scope.cats = [];
$scope.users = [{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }];
//For example 2
//For example 2 (requires 'ropooy-angular-lodash/utils')
$scope.getUserNames = function() {
return $scope.pluck($scope.users, 'user');
};
//For example 3 / (requires 'ropooy-angular-lodash/service')
// Why? because '_'.length < '$scope'.length
// and I hate to use globals inside AngularJS controllers / directives / etc.
$scope.getNames = function() {
return _.pluck($scope.users, 'user');
};
});
</script>
```
Expand All @@ -42,14 +50,15 @@ LoDash methods listed in 'adapList' array can be used as [AngularJS filter](http
```html
<body ng-app="myApp">
<div ng-controller="MyCtrl">
<!-- requires 'ropooy-angular-lodash/filters' -->
<!-- output unique numbers from input.. [1, 2, 3, 4, 5, 6, 7, 8] -->
<div ng-repeat="num in [1,1,2,3,4,5,5,6,6,7,7,7,8]|uniq">{{num}}</div>
</div>
</body>
```

#### 2. Use defined utils
All methods defined in LoDash API can be used inside view and controller as utilitity.
All methods defined in LoDash API can be used inside view, controller and directive, because those are bind to $scope. (or in filters and services through $rootScope, but prefer using DI service!)
```html
<body ng-app="myApp">
<div ng-controller="MyCtrl">
Expand All @@ -60,4 +69,14 @@ All methods defined in LoDash API can be used inside view and controller as util
</body>
```

#### 3. Use defined _ DI service
Cleaner way to use LoDash inside angular directives, services, controllers or even inside filters.
```html
<body ng-app="myApp">
<div ng-controller="MyCtrl">
<span ng-repeat="name in getNames()">{{name}}</span>
</div>
</body>
```

### Note: not available through bower, yet.
16 changes: 11 additions & 5 deletions angular-lodash.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(function (ng, _) {
'use strict';

var
lodashModule = ng.module('ropooy-angular-lodash', []),
var lodashModule = ng.module('ropooy-angular-lodash', ['ropooy-angular-lodash/service']),
utilsModule = ng.module('ropooy-angular-lodash/utils', []),
filtersModule = ng.module('ropooy-angular-lodash/filters', []);
filtersModule = ng.module('ropooy-angular-lodash/filters', []),
diModule = ng.module('ropooy-angular-lodash/service', []);

// begin custom _

Expand Down Expand Up @@ -57,6 +57,12 @@

// end custom _

// begin ropooy-angular-lodash/service
diModule.factory('_', ['$window', function($window) {
return $window._;
}]);
// end ropooy-angular-lodash/service


// begin register ropooy-angular-lodash/utils

Expand Down Expand Up @@ -95,6 +101,7 @@
'countBy',
'shuffle',
'toArray',
'has',
'size',
['first', 'head', 'take'],
'initial',
Expand Down Expand Up @@ -132,8 +139,7 @@
filterNames = [filterNames];
}

var
filter = _.bind(_[filterNames[0]], _),
var filter = _.bind(_[filterNames[0]], _),
filterFactory = function() {return filter;};

_.each(filterNames, function(filterName) {
Expand Down
10 changes: 9 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
'use strict';

var gulp = require('gulp');
var KarmaServer = require('karma').Server;
var jshint = require('gulp-jshint');

gulp.task('jshint', function() {
gulp.src(['gulpfile.js', './test/**/*'])
gulp.src(['gulpfile.js', 'karma.config.js', './test/**/*.js'])
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('jshint-stylish'));
});

gulp.task('test', ['jshint'], function() {
new KarmaServer({
configFile: __dirname + '/karma.config.js',
singleRun: true
}).start();
});

gulp.task('default', ['jshint']);
})();
54 changes: 54 additions & 0 deletions karma.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html

'use strict';

module.exports = function (config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: './',

// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],

// list of files / patterns to load in the browser
files: [
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'node_modules/lodash/lodash.js',

'angular-lodash.js',
'test/apiSpec.js',
'test/serviceSpec.js',
'test/ngFilterSpec.js',
'test/filterSpec.js'
],

preprocessors: {/*None*/},

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

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


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

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['PhantomJS'],

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
});
};
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@
"url": "https://github.com/RopoMen/ropooy-angular-lodash.git"
},
"scripts": {
"test": "gulp jshint"
"test": "gulp test"
},
"devDependencies": {
"angular": "^1.4.3",
"angular-mocks": "^1.4.3",
"gulp": "^3.9.0",
"gulp-jshint": "^1.11.2",
"jshint-stylish": "^2.0.1"
"jasmine-core": "^2.3.4",
"jshint-stylish": "^2.0.1",
"karma": "^0.13.3",
"karma-jasmine": "^0.3.6",
"karma-phantomjs-launcher": "^0.2.0",
"lodash": "2.4.1"
}
}
29 changes: 23 additions & 6 deletions test/apiSpec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
'use strict';

describe('ropooy-angular-lodash: API', function() {
beforeEach(module('_'));
_.each(_.functions(_), function(fnName) {
it(fnName + ' should adapt to lodash\'s '+ fnName, inject(function($rootScope) {
expect($rootScope[fnName].toString()).toBe(_[fnName].toString());
describe('ropooy-angular-lodash: API', function () {
beforeEach(module('ropooy-angular-lodash/utils'));

var $window,
$rootScope;

beforeEach(inject(function (_$window_, _$rootScope_) {
$window = _$window_;
$rootScope = _$rootScope_;
}));
});

describe('Testing LoDash methods', function () {
it('Should be a function ($window._)', inject(function () {
expect(angular.isFunction($window._)).toBe(true);
}));

var testData = _.functions(_);

angular.forEach(testData, function(fnName) {
it('Should match LoDash ' + fnName + ' -method in to $rootScope', function() {
expect(angular.isFunction($rootScope[fnName])).toBe(true);
});
});
});
});
Loading

0 comments on commit b78a42b

Please # to comment.