Comparing version 1.2.0 to 1.3.0
12
app.js
@@ -1,1 +0,11 @@ | ||
$.Del = require('./del.js'); | ||
$.Del = require('./del.js'); | ||
mocha.setup('bdd'); | ||
window.spies = require('chai-spies'); | ||
chai.use(spies); | ||
window.spy = chai.spy; | ||
window.assert = chai.assert, | ||
window.expect = chai.expect; | ||
window.should = chai.should(); | ||
window.l = function (x) { | ||
console.log(x); | ||
} |
89
del.js
@@ -14,10 +14,37 @@ var defaults = { | ||
/** | ||
* @typedef {String} dName | ||
* | ||
* Element document name. | ||
* This name is used as an identifier for element. | ||
*/ | ||
module.exports = { | ||
/** | ||
* Init del module | ||
* @param {Object} options | ||
* @param {String} [options.dName] | ||
* @param {String} [options.namespace] Namespace | ||
* @param {jQuery|DOMElement} [options.$el] | ||
*/ | ||
initDel: function(options) { | ||
this.DelOptions = $.extend({}, defaults, options); | ||
this.dName = this.dName || this._name; | ||
this.DelOptions = $.extend({}, defaults, this.DelOptions, options); | ||
this.dName = this.DelOptions.dName || this._name; | ||
this.selector = this.selector || '.' + this.dName; | ||
this.namespace = this.DelOptions.namespace || this.dName; | ||
this.initEl(); | ||
}, | ||
/** | ||
* Define $el | ||
*/ | ||
initEl: function() { | ||
if (this.$el) return; | ||
if (this.DelOptions.$el) { | ||
if (this.DelOptions.$el instanceof $) | ||
this.$el = this.DelOptions.$el; | ||
else this.$el = $(this.DelOptions.$el); | ||
} else this.$el = $('.' + this.dName); | ||
}, | ||
makeName: function(elName, modName) { | ||
@@ -44,6 +71,9 @@ var name = this.dName; | ||
/** | ||
* Find child element by a dName | ||
* @param {String} dName dName | ||
* @return {jQuery} Finded element | ||
*/ | ||
find: function() { | ||
return this._smartArgs(function($el, args, context) { | ||
return $el.find(context.makeSelector.apply(context, args)); | ||
}, arguments); | ||
return this.$el.find(this.makeSelector.apply(this, arguments)); | ||
}, | ||
@@ -73,42 +103,21 @@ | ||
hasMod: function(name) { | ||
return this._smartArgs(function($el, args, context) { | ||
return $el.hasClass(context.modName(args[0])); | ||
}, arguments); | ||
return this.$el.hasClass(this.modName(name)); | ||
}, | ||
/** | ||
* Filter function that defines first argument for methods. | ||
* @param {Function} callback callback | ||
* @param {arguments} args Arguments of first method | ||
* @return {n/a} | ||
*/ | ||
_smartArgs: function(callback, args) { | ||
if (args[0] instanceof $) | ||
return callback(args[0], [].slice.call(args, 1), this); | ||
else | ||
return callback(this[this.DelOptions.$elName], args, this); | ||
addMod: function(name) { | ||
return this.$el.addClass(this.modName(name)); | ||
}, | ||
addMod: function(name) { | ||
this._smartArgs(function($el, args, context) { | ||
$el.addClass(context.modName(args[0])); | ||
}, arguments); | ||
}, | ||
filterByMod: function(name) { | ||
return this._smartArgs(function($el, args, context) { | ||
return $el.filter('.' + context.modName(args[0])); | ||
}, arguments); | ||
return this.$el.filter('.' + this.modName(name)); | ||
}, | ||
removeMod: function(name) { | ||
this._smartArgs(function($el, args, context) { | ||
$el.removeClass(context.modName(args[0])); | ||
}, arguments); | ||
this.$el.removeClass(this.modName(name)); | ||
}, | ||
toggleMod: function(name, state) { | ||
this._smartArgs(function($el, args, context) { | ||
args[0] = context.modName(args[0]); | ||
$.fn.toggleClass.apply($el, args); | ||
}, arguments); | ||
var args = arguments; | ||
args[0] = this.modName(args[0]); | ||
$.fn.toggleClass.apply(this.$el, args); | ||
}, | ||
@@ -133,5 +142,3 @@ | ||
args[0] = this.eventName(name); | ||
this._smartArgs(function($el, args, context) { | ||
$.fn.on.apply($el, args); | ||
}, args); | ||
$.fn.on.apply(this.$el, args); | ||
}, | ||
@@ -143,10 +150,6 @@ | ||
off: function(name) { | ||
this._smartArgs(function($el, args, context) { | ||
$el.off(context.eventName(name)); | ||
}, arguments); | ||
this.$el.off(this.eventName(name)); | ||
}, | ||
trigger: function(name) { | ||
this._smartArgs(function($el, args, context) { | ||
$el.trigger(context.eventName(name)); | ||
}, arguments); | ||
this.$el.trigger(this.eventName(name)); | ||
}, | ||
@@ -153,0 +156,0 @@ |
@@ -39,3 +39,4 @@ var gulp = require('gulp'), | ||
}, | ||
files: ['./test/*', 'del.js'] | ||
files: ['./test/*', 'del.js'], | ||
open: false | ||
}); | ||
@@ -42,0 +43,0 @@ }); |
{ | ||
"name": "ya-del", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "", | ||
@@ -8,4 +8,3 @@ "main": "del.js", | ||
"test": "echo \"No tests specified\" && exit 0", | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post", | ||
"commit": "git-cz" | ||
"semantic-release": "semantic-release pre && npm publish && semantic-release post" | ||
}, | ||
@@ -22,6 +21,5 @@ "repository": { | ||
"chai": "^3.5.0", | ||
"commitizen": "^2.5.0", | ||
"cz-conventional-changelog": "^1.1.5", | ||
"chai-spies": "^0.7.1", | ||
"ghooks": "^1.0.3", | ||
"gulp": "^3.9.0", | ||
"gulp": "^3.9.1", | ||
"gulp-util": "^3.0.7", | ||
@@ -35,2 +33,2 @@ "gulp-watch": "^4.3.5", | ||
} | ||
} | ||
} |
109
test/test.js
@@ -0,7 +1,23 @@ | ||
var $fixture; | ||
beforeEach(function() { | ||
$fixture = $('#fixture').empty(); | ||
}); | ||
describe('#initDel', function() { | ||
it ('have all properties', function() { | ||
var module = makeModule(); | ||
assert(module.dName == 'module'); | ||
assert(module.namespace == 'module'); | ||
it('call #initEl', function() { | ||
var module = makeSimpleModule(); | ||
var spiedInitEl = spy.on(module, 'initEl'); | ||
var $el = makeEl('module').appendTo($fixture); | ||
module.initDel({ | ||
dName: 'module' | ||
}); | ||
spiedInitEl.should.have.been.called(); | ||
}); | ||
it('set DelOptions', function() { | ||
var module = makeSimpleModule(); | ||
module.initDel({key: null}); | ||
expect(module.DelOptions).to.have.property('key') | ||
}); | ||
}); | ||
@@ -137,27 +153,2 @@ | ||
describe('#_smartArgs', function() { | ||
beforeEach(function() { | ||
this.module = makeModule(); | ||
}); | ||
it ('first arguments is jQuery instance', function() { | ||
var $_el = $('<div />'); | ||
var module = this.module; | ||
this.module._smartArgs(function($el, args, context) { | ||
assert($el instanceof $); | ||
assert($_el[0] == $el[0]); | ||
assert(args[0] == 'string'); | ||
assert(context == module); | ||
}, [$_el, 'string']); | ||
}); | ||
it('first argument is not jQuery instance', function() { | ||
var module = this.module; | ||
this.module._smartArgs(function($el, args, context) { | ||
assert(module.$el[0] == $el[0]); | ||
assert(args[0] == 'str1'); | ||
assert(args[1] == 'str2'); | ||
}, ['str1', 'str2']); | ||
}); | ||
}); | ||
describe('modifier', function() { | ||
@@ -210,13 +201,7 @@ var module; | ||
}); | ||
it('with custom el', function() { | ||
var $_el = $('<div />'); | ||
this.module.toggleMod($_el, 'mod'); | ||
assert($_el.hasClass('module--mod')); | ||
}); | ||
}); | ||
it('#filterByMod', function() { | ||
var $el = $('<div class="module--mod"></div>'); | ||
this.module.$el.append($el); | ||
assert(this.module.filterByMod($el, 'mod')[0] == $el[0]); | ||
this.module.$el.addClass('module--mod'); | ||
assert(this.module.filterByMod('mod')[0] == this.module.$el[0]); | ||
}); | ||
@@ -239,8 +224,46 @@ }); | ||
}); | ||
describe('#initEl', function() { | ||
it('exit if $el exists', function() { | ||
var module = makeSimpleModule(); | ||
var $el = makeEl(); | ||
module.$el = $el; | ||
module.initDel(); | ||
module.$el[0].should.be.eql($el[0]); | ||
}); | ||
it('set $el prop from DelOptions', function() { | ||
var module = makeSimpleModule(); | ||
var $el = $('<div />'); | ||
module.DelOptions = {$el: $el}; | ||
module.initEl(); | ||
expect(module.$el[0]).to.be.eql($el[0]); | ||
}); | ||
it('make $el by dName if $el is not provided', function() { | ||
var module = makeSimpleModule(); | ||
var $el = $('<div />', {'class': 'dName'}); | ||
$fixture.append($el); | ||
module.DelOptions = {}; | ||
module.dName = 'dName'; | ||
module.initEl(); | ||
expect(module.$el[0]).to.be.eql($el[0]); | ||
}); | ||
it('transform $el to jQuery if it is not', function() { | ||
var module = makeSimpleModule(); | ||
var $el = $('<div />'); | ||
$fixture.append($el); | ||
module.DelOptions = {$el: $el[0]}; | ||
module.initEl(); | ||
expect(module.$el[0]).to.be.eql($el[0]); | ||
}); | ||
}); | ||
function makeModule(name, $el) { | ||
name = name || 'module'; | ||
var Module = function(){ | ||
this.dName = name; | ||
this.$el = $el ? $el : $('<div class="'+ name +'"></div>'); | ||
this.initDel(); | ||
this.$el = makeEl(name); | ||
this.initDel({dName: name}); | ||
}; | ||
@@ -252,2 +275,8 @@ Module.prototype = $.Del; | ||
function makeSimpleModule () { | ||
function Module () {} | ||
Module.prototype = Object.create($.Del); | ||
return new Module; | ||
} | ||
function makeEl(className) { | ||
@@ -254,0 +283,0 @@ var $el = $('<div />', { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
13
0
17442
13
474