New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

off

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

off - npm Package Compare versions

Comparing version 0.0.8 to 0.0.10

36

Gruntfile.js
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
shell: {
uglify: {
command: 'uglifyjs off.js --mangle --compress -o off-min.js'
},
bumpup: {
command: 'mversion patch -m'
},
test: {
command: 'jasmine'
}
},
shell: {
uglify: {
command: 'uglifyjs off.js --mangle --compress -o off-min.js'
},
bumpup: {
command: 'mversion patch -m'
},
test: {
command: 'jasmine'
}
},
});
});
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('build', ['shell:test','shell:uglify']);
grunt.registerTask('bumpup', ['shell:bumpup']);
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('build', ['shell:test','shell:uglify']);
grunt.registerTask('bumpup', ['shell:bumpup']);
};

@@ -1,1 +0,1 @@

!function(n,r){"function"==typeof define&&define.amd?define(function(){return n.off=r()}):"object"==typeof module&&module.exports?module.exports=n.off=r():n.off=r()}(this,function(){var n=function(n,r){var t=[],e=[];n=n||function(){};var o=function(){var n,u,f,i=r||this;return n=Array.prototype.slice.call(arguments,0),(u=e.some(function(r){return r.apply(this,n)}))?void 0:(f=o.func.apply(i,n),o.lock?o.lock=!1:t.forEach(function(r){f instanceof Function&&f._off?f.add(r):r.apply(i,[f,n])}),f)};return o.before=function(n,r){return r=r||{},(-1===e.indexOf(n)||r.force_add)&&e.push(n),o},o.add=function(n,r){return r=r||{},(-1===t.indexOf(n)||r.force_add)&&t.push(n),o},o.override=function(n){var r=o.func;o.func=function(){var t=Array.prototype.slice.call(arguments,0);return t.unshift(r.bind(this)),n.apply(this,t)}},o.remove=function(n){var r=t.indexOf(n);-1!==r&&t.splice(r,1)},o._off=!0,o.func=n,o};return n.signal=function(){return n(function(n){return n})},n.property=function(r,t){var e,o,u=r,f={};return t=t||function(n,r){return r()===n&&(r.property.lock=!0),r(n)},e=n(function(n,r){return 0===arguments.length&&r!==f?(e.lock=!0,u):t(n,o)}),e.bind=function(n){e.add(n),void 0!==e()&&n(e())},e.reset=function(){return e(f)},o=function(n){return arguments.length&&(u=n),u},o.property=e,e.property=!0,e},n.async=function(r,t){var e=null;return n(function(){var o;t&&e&&(e.lock=!0),e=o=n(function(n){return n},this);var u=Array.prototype.slice.call(arguments,0).concat(o);return r.apply(this,u),o})},n.deferred=function(r,t){var e,o,u,f=!1,i=null,c=n.signal();return o=n(function(){return i=Array.prototype.slice.call(arguments,0),e=c,f?c:(f=!0,t(function(){u=r.apply(this,i),f=!1,c(u)}),c)})},n.decorate=function(r){for(var t in r)"function"!=typeof r[t]||r[t]._off||(r[t]=n(r[t]));return r},n});
!function(n,t){"function"==typeof define&&define.amd?define(function(){return n.off=t()}):"object"==typeof module&&module.exports?module.exports=n.off=t():n.off=t()}(this,function(){function n(n,t,o){var r=-1;return n.forEach(function(n,e){n.method===t&&n.context===o&&(r=e)}),r}var t=function(o){var r=[],e=[],c={};o=o||function(){};var u=function(){var n,t,o;return n=Array.prototype.slice.call(arguments,0),(t=e.some(function(t){return t.method.apply(t.context,n)}))?void 0:(o=u.func.apply(this,n),u.lock?u.lock=!1:(u.last=o,u._dispatch(o)),o)};return u._dispatch=function(n){r.forEach(function(t){n instanceof Function&&n._off?n.bind(t.method,t.context):t.method.call(t.context,n)})},u.before=function(t,o){return-1===n(e,t,o)&&e.push({method:t,context:o}),u},u.before.remove=function(t,o){var r=n(e,t,o);-1!==r&&e.splice(r,1)},u.add=function(t,o){return-1===n(r,t,o)&&r.push({method:t,context:o}),u},u.bind=function(n,t){void 0!==u.last&&n.call(t,u.last),u.add(n,t)},u.remove=function(t,o){var e=n(r,t,o);-1!==e&&r.splice(e,1)},u.override=function(n){var t=u.func;u.func=function(){var o=Array.prototype.slice.call(arguments,0);return o.unshift(t.bind(this)),n.apply(this,o)}},u.last=void 0,u.as=function(n){return c[n]=c[n]||t(u),c[n]},u._off=!0,u.func=o,u};return t.signal=function(){var n;return n=t(function(n){return n})},t.property=function(n){var o=t(function(n){return 1===arguments.length&&n!==o.last?n:(o.lock=!0,o.last)});return o.last=n,o},t.async=function(n){var o,r=t.signal();return o=t(function(){var t=[r].concat(Array.prototype.slice.call(arguments,0));return n.apply(this,t),r})},t.decorate=function(n){for(var o in n)"function"!=typeof n[o]||n[o]._off||(n[o]=t(n[o]));return n},t});
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(function () {
return (root.off = factory());
});
} else if (typeof module === "object" && module.exports) {
module.exports = (root.off = factory());
} else {
root.off = factory();
}
if (typeof define === "function" && define.amd) {
define(function () {
return (root.off = factory());
});
} else if (typeof module === "object" && module.exports) {
module.exports = (root.off = factory());
} else {
root.off = factory();
}
})(this, function () {
var off = function (func, context) {
var _handlers = [],
_before = [];
func = func || function () {};
function find(handlers, method, context) {
var index = -1;
handlers.forEach(function(handler, i) {
if (handler.method === method && handler.context === context) {
index = i;
}
});
return index;
}
var runner = function () {
var self = context || this,
args, blocked, result;
var off = function (func) {
var _handlers = [],
_before = [],
_scopes = {};
args = Array.prototype.slice.call(arguments, 0);
func = func || function () {};
blocked = _before.some(function (handler) {
return handler.apply(this, args);
});
var runner = function () {
var args, blocked, result;
if (blocked) {
return;
}
args = Array.prototype.slice.call(arguments, 0);
result = runner.func.apply(self, args);
blocked = _before.some(function (handler) {
return handler.method.apply(handler.context, args);
});
if (runner.lock) {
runner.lock = false;
} else {
_handlers.forEach(function (handler) {
if (result instanceof Function && result._off) {
result.add(handler);
} else {
handler.apply(self, [result, args]);
}
});
}
if (blocked) {
return;
}
return result;
};
result = runner.func.apply(this, args);
runner.before = function (handler, options) {
options = options || {};
if (_before.indexOf(handler) === -1 || options.force_add) {
_before.push(handler);
}
return runner;
};
if (runner.lock) {
runner.lock = false;
} else {
runner.last = result;
runner._dispatch(result);
}
runner.add = function (handler, options) {
options = options || {};
if (_handlers.indexOf(handler) === -1 || options.force_add) {
_handlers.push(handler);
}
return runner;
};
return result;
};
runner.override = function(override) {
var func = runner.func;
runner.func = function() {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(func.bind(this));
return override.apply(this, args);
}
};
runner._dispatch = function(result) {
_handlers.forEach(function (handler) {
if (result instanceof Function && result._off) {
result.bind(handler.method, handler.context);
} else {
handler.method.call(handler.context, result);
}
});
};
runner.remove = function (handler) {
var index = _handlers.indexOf(handler);
if (index !== -1) {
_handlers.splice(index, 1);
}
};
runner.before = function (method, context) {
if (find(_before, method, context) === -1) {
_before.push({method: method, context: context});
}
return runner;
};
runner._off = true;
runner.before.remove = function(method, context) {
var index = find(_before, method, context);
if (index !== -1) {
_before.splice(index, 1);
}
};
runner.func = func;
runner.add = function (method, context) {
if (find(_handlers, method, context) === -1) {
_handlers.push({method: method, context: context});
}
return runner;
};
return runner;
};
runner.bind = function(method, context) {
if (runner.last !== undefined) {
method.call(context, runner.last);
}
runner.add(method, context);
};
off.signal = function () {
return off(function (value) {
return value;
});
};
runner.remove = function (method, context) {
var index = find(_handlers, method, context);
if (index !== -1) {
_handlers.splice(index, 1);
}
};
off.property = function (initial_value, setter) {
var _value = initial_value,
property, $guard, _reset = {};
runner.override = function(override) {
var func = runner.func;
runner.func = function() {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(func.bind(this));
return override.apply(this, args);
}
};
setter = setter || function (value, guard) {
if (guard() === value) {
guard.property.lock = true;
}
return guard(value);
};
runner.last = undefined;
property = off(function (value, reset) {
if (arguments.length === 0 && reset !== _reset) {
property.lock = true;
return _value;
} else {
return setter(value, $guard);
}
});
property.bind = function (handler) {
property.add(handler);
if (property() !== undefined) {
handler(property());
}
};
property.reset = function () {
return property(_reset);
};
runner.as = function(name) {
_scopes[name] = _scopes[name] || off(runner);
return _scopes[name];
};
$guard = function (value) {
if (arguments.length) {
_value = value;
}
return _value;
};
$guard.property = property;
property.property = true;
runner._off = true;
return property;
};
runner.func = func;
off.async = function (func, throttle) {
var last_callback = null;
return off(function () {
var callback;
if (throttle && last_callback) {
last_callback.lock = true;
}
last_callback = callback = off(function (value) {return value}, this);
var args = Array.prototype.slice.call(arguments, 0).concat(callback);
func.apply(this, args);
return callback;
});
};
return runner;
};
off.deferred = function (func, deferring_function) {
var _pending = false,
last_args = null,
final_callback = off.signal(),
$final_callback,
deferred, result;
deferred = off(function () {
last_args = Array.prototype.slice.call(arguments, 0);
$final_callback = final_callback;
if (!_pending) {
_pending = true;
deferring_function(function () {
result = func.apply(this, last_args);
_pending = false;
final_callback(result);
});
return final_callback;
}
return final_callback;
});
off.signal = function () {
var result;
result = off(function (value) {
return value;
});
return result;
};
return deferred;
};
off.property = function (default_value) {
var property = off(function(value){
if (arguments.length === 1 && value !== property.last) {
return value;
}
else {
property.lock = true;
return property.last;
}
});
off.decorate = function (obj) {
for (var property in obj) {
if (typeof (obj[property]) === "function" && ! (obj[property]._off)) {
obj[property] = off(obj[property]);
}
}
return obj;
};
property.last = default_value;
return off;
return property;
};
off.async = function(func) {
var done = off.signal(), result;
result = off(function(){
var args = [done].concat(Array.prototype.slice.call(arguments, 0));
func.apply(this, args);
return done;
});
return result;
};
off.decorate = function (obj) {
for (var property in obj) {
if (typeof (obj[property]) === "function" && ! (obj[property]._off)) {
obj[property] = off(obj[property]);
}
}
return obj;
};
return off;
});
{
"name": "off",
"version": "0.0.8",
"version": "0.0.10",
"description": "Observable functions",

@@ -5,0 +5,0 @@ "main": "off.js",

describe('off', function () {
var off = require('../off'),
handler,
fn,
async,
throttled_async;
var off = require('../off'),
handler,
fn,
async;
beforeEach(function () {
handler = jasmine.createSpy();
fn = off(function () {});
async = off.async(function (callback) {
setTimeout(callback, 10);
});
beforeEach(function () {
handler = jasmine.createSpy();
fn = off(function () {});
async = off.async(function (callback) {
setTimeout(callback, 10);
});
throttled_async = off.async(function (callback) {
setTimeout(callback, 10);
}, true);
});
});
it('should run handlers after running wrapped function', function () {
fn.add(handler);
expect(handler).not.toHaveBeenCalled();
it('should run handlers after running wrapped function', function () {
fn.add(handler);
expect(handler).not.toHaveBeenCalled();
fn();
expect(handler).toHaveBeenCalled();
});
fn();
expect(handler).toHaveBeenCalled();
});
it('should remove handlers', function () {
fn.add(handler);
expect(handler).not.toHaveBeenCalled();
it('should remove handlers', function () {
fn.add(handler);
expect(handler).not.toHaveBeenCalled();
fn();
expect(handler).toHaveBeenCalled();
fn();
expect(handler).toHaveBeenCalled();
fn.remove(handler);
fn();
expect(handler.calls.count()).toEqual(1);
});
fn.remove(handler);
fn();
expect(handler.calls.count()).toEqual(1);
});
it('should run handlers for async calls', function (done) {
async.add(handler);
it('should handle context', function() {
async();
var object = {
handler: function(r) {
this.result = r;
}
};
setTimeout(function () {
expect(handler).toHaveBeenCalled();
done();
}, 20);
var source = {
value: 2,
fire: off(function(){
return this.value;
})
};
});
source.fire.add(object.handler, object);
source.fire();
it('should lock handlers if function is locked', function () {
var locker = off(function (value) {
if (value) {
locker.lock = true;
}
});
expect(object.result).toBe(2);
});
locker.add(handler);
it('should add/remove handlers by context', function() {
locker(false);
locker(true);
var a = {
handler: jasmine.createSpy()
};
expect(handler.calls.count()).toEqual(1);
});
var b = {
handler: jasmine.createSpy()
};
it('should lock handlers if async function is locked', function (done) {
var async_locker = off(function (value) {
var result_callback = off.signal();
if (value) {
async_locker.lock = true;
}
setTimeout(result_callback, 10);
return result_callback;
});
fn.add(a.handler, a);
fn.add(b.handler, b);
async_locker.add(handler);
fn.remove(a.handler, a);
fn.remove(b.handler, {});
async_locker(false);
async_locker(true);
fn();
setTimeout(function () {
expect(handler.calls.count()).toEqual(1);
done();
}, 40);
});
expect(a.handler).not.toHaveBeenCalled();
expect(b.handler).toHaveBeenCalled();
});
it('should not allow to add same handler multiple times', function () {
fn.add(handler);
fn.add(handler);
it('should run handlers for async calls', function (done) {
async.add(handler);
fn();
expect(handler.calls.count()).toEqual(1);
async();
});
setTimeout(function () {
expect(handler).toHaveBeenCalled();
done();
}, 20);
it('should run only the last async callback when called multiple times', function (done) {
throttled_async.add(handler);
});
throttled_async();
throttled_async();
throttled_async();
it('should lock handlers if function is locked', function () {
var locker = off(function (value) {
if (value) {
locker.lock = true;
}
});
setTimeout(function () {
expect(handler.calls.count()).toEqual(1);
done();
}, 20);
});
locker.add(handler);
it('should correctly handle context of the sync handler', function () {
var context = {
action: fn,
handler: function () {
expect(this).toEqual(context);
}
};
locker(false);
locker(true);
context.action.add(context.handler);
context.action();
});
expect(handler.calls.count()).toEqual(1);
});
it('should correctly handle context of the async handler', function (done) {
var context = {
action: async,
handler: function () {
expect(this).toEqual(context);
done();
}
};
it('should lock handlers if async function is locked', function (done) {
var async_locker = off(function (value) {
var result_callback = off.signal();
if (value) {
async_locker.lock = true;
}
setTimeout(result_callback, 10);
return result_callback;
});
context.action.add(context.handler);
context.action();
});
async_locker.add(handler);
it('should allow making a deffered call', function (done) {
var action = function (value) {
expect(value).toEqual(10);
return 10;
};
var handler = function (value) {
expect(value).toEqual(10);
done();
}
var deferred = off.deferred(action, function (fn) {
setTimeout(fn, 40);
});
async_locker(false);
async_locker(true);
deferred.add(handler);
setTimeout(function () {
expect(handler.calls.count()).toEqual(1);
done();
}, 40);
});
deferred(1);
deferred(5);
deferred(10);
it('should not allow to add same handler multiple times', function () {
fn.add(handler);
fn.add(handler);
});
fn();
expect(handler.calls.count()).toEqual(1);
it('should allow to add a before handler', function () {
var result = '';
var before = function () {
result += 'a';
};
var action = off(function () {
result += 'b';
});
var after = function () {
result += 'c';
};
});
action.add(after);
action.before(before);
it('should run all handlers for async calls', function(done){
var handler2 = jasmine.createSpy();
async.add(handler);
async.add(handler2);
action();
async();
expect(result).toEqual("abc");
});
setTimeout(function(){
expect(handler.calls.count()).toEqual(1);
expect(handler2.calls.count()).toEqual(1);
done();
}, 30);
});
it('should allow to lock an action', function () {
var result = '';
var before = function () {
result += 'a';
return true;
};
var action = off(function () {
result += 'b';
});
var after = function () {
result += 'c';
};
it('should allow to add a before/after handler', function () {
var result = '';
var before = function () {
result += 'a';
};
var action = off(function () {
result += 'b';
});
var after = function () {
result += 'c';
};
action.add(after);
action.before(before);
action.add(after);
action.before(before);
action();
action();
expect(result).toEqual("a");
});
expect(result).toEqual("abc");
});
it('should decorate all object methods', function() {
var test = function() {};
it('should allow to lock an action', function () {
var result = '';
var before = function () {
result += 'a';
return true;
};
var action = off(function () {
result += 'b';
});
var after = function () {
result += 'c';
};
var obj = {
foo: 'bar',
test: test
};
action.add(after);
action.before(before);
var obj = off.decorate(obj);
action();
expect(obj.foo).toEqual('bar');
expect(obj.test).not.toBe(test);
expect(obj.test._off).toBe(true);
});
expect(result).toEqual("a");
});
it('should not decorate already wrapped methods', function() {
var wrapped_test = off(function(){});
it('should decorate all object methods', function() {
var test = function() {};
var obj = {
wrapped_test: wrapped_test
};
var obj = {
foo: 'bar',
test: test
};
obj = off.decorate(obj);
expect(obj.wrapped_test).toBe(wrapped_test);
});
obj = off.decorate(obj);
it('should allow to override functions', function(){
var base = {
create_text: off(function(value){
this.text = value;
return value;
})
};
expect(obj.foo).toEqual('bar');
expect(obj.test).not.toBe(test);
expect(obj.test._off).toBe(true);
});
var foo = Object.create(base);
foo.create_text.override(function($super, value){
this.text = 'foo: ' + $super(value);
return this.text;
});
it('should not decorate already wrapped methods', function() {
var wrapped_test = off(function(){});
var bar = Object.create(foo);
bar.create_text.override(function($super, value){
this.text = $super(value) + '!';
return this.text;
});
var obj = {
wrapped_test: wrapped_test
};
bar.create_text('test');
expect(bar.text).toBe('foo: test!');
});
obj = off.decorate(obj);
expect(obj.wrapped_test).toBe(wrapped_test);
});
it('should pass result and args to handlers', function() {
var method = off(function(a) {
return a + 1;
});
it('should allow to override functions', function(){
var base = {
create_text: off(function(value){
this.text = value;
return value;
})
};
method.add(handler);
method(1);
var foo = Object.create(base);
foo.create_text.override(function($super, value){
this.text = 'foo: ' + $super(value);
return this.text;
});
expect(handler).toHaveBeenCalledWith(2, [1]);
});
var bar = Object.create(foo);
bar.create_text.override(function($super, value){
this.text = $super(value) + '!';
return this.text;
});
describe('signal', function () {
bar.create_text('test');
expect(bar.text).toBe('foo: test!');
});
var signal;
it('should pass result and args to handlers', function() {
var method = off(function(a) {
return a + 1;
});
beforeEach(function () {
signal = off.signal();
});
method.add(handler);
method(1);
it('should pass value passed when exeucting signal', function () {
signal.add(handler);
expect(handler).toHaveBeenCalledWith(2);
});
signal('test');
describe('signal', function () {
expect(handler).toHaveBeenCalledWith('test', jasmine.any(Object));
var signal;
});
beforeEach(function () {
signal = off.signal();
});
});
it('should pass value passed when exeucting signal', function () {
signal.add(handler);
describe('property', function () {
signal('test');
var property;
expect(handler).toHaveBeenCalledWith('test');
beforeEach(function () {
property = off.property();
});
});
it('should store the last value', function () {
property(10);
expect(property()).toEqual(10);
property('test');
property('test2');
expect(property()).toEqual('test2');
});
it('should run handler added with bind() if signal has been already dispatched', function () {
it('should run handler only when setting the value', function () {
property.add(handler);
signal(20);
property(10);
property();
signal.bind(handler);
expect(handler.calls.count()).toEqual(1);
});
expect(handler).toHaveBeenCalledWith(20);
});
it('should run handlers only when value changes', function () {
property.add(handler);
});
property(10);
property(11);
property(11);
describe('scopes', function() {
expect(handler.calls.count()).toEqual(2);
});
it('should cache scoped methods', function() {
var base = off.signal(),
foo = base.as('foo'),
foo2 = base.as('foo'),
bar = base.as('bar');
it('should accept custom setters', function () {
var increment = off.property(0, function (value, guard) {
guard(guard() + value);
});
expect(foo).toEqual(foo2);
expect(foo).not.toEqual(bar);
});
increment(2);
expect(increment()).toEqual(2);
it('should create cascaded scopes', function() {
var base = off.signal(),
foo = base.as('foo'),
foobar = base.as('foo').as('bar');
increment(3);
expect(increment()).toEqual(5);
var base_spy = jasmine.createSpy(),
foo_spy = jasmine.createSpy(),
foobar_spy = jasmine.createSpy();
});
base.add(base_spy);
foo.add(foo_spy);
foobar.add(foobar_spy);
it('should run handler added with bind() if value is defined', function () {
base('BASE');
expect(base_spy).toHaveBeenCalledWith('BASE');
expect(foo_spy).not.toHaveBeenCalledWith('BASE');
expect(foobar_spy).not.toHaveBeenCalledWith('BASE');
property(20);
foo('FOO');
expect(base_spy).toHaveBeenCalledWith('FOO');
expect(foo_spy).toHaveBeenCalledWith('FOO');
expect(foobar_spy).not.toHaveBeenCalledWith('FOO');
property.bind(handler);
foobar('FOOBAR');
expect(base_spy).toHaveBeenCalledWith('FOOBAR');
expect(foo_spy).toHaveBeenCalledWith('FOOBAR');
expect(foobar_spy).toHaveBeenCalledWith('FOOBAR');
});
expect(handler).toHaveBeenCalledWith(20);
});
});
it('should allow to reset the value to re-run all handlers', function () {
property.add(handler);
describe('property', function () {
property(11);
property(11);
var property;
expect(handler.calls.count()).toEqual(1);
beforeEach(function () {
property = off.property();
});
property.reset();
expect(handler.calls.count()).toEqual(2);
});
});
it('should store the last value', function () {
property(10);
expect(property()).toEqual(10);
})
property('test');
property('test2');
expect(property()).toEqual('test2');
});
it('should run handler only when setting the value', function () {
property.add(handler);
property(10);
property();
expect(handler.calls.count()).toEqual(1);
});
it('should run handlers only when value changes', function () {
property.add(handler);
property(10);
property(11);
property(11);
expect(handler.calls.count()).toEqual(2);
});
it('should run handler added with bind() if value is defined', function () {
property(20);
property.bind(handler);
expect(handler).toHaveBeenCalledWith(20);
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc