Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tiny-totalizer

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-totalizer - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

test/core.test.coffee

3

example.js

@@ -35,2 +35,3 @@ #!/usr/bin/env node

allowMinus: true, // 0未満になる
calcReturnIsKey: true, // add/subの戻り値をキー名にする
standardizer: function (s) { // キー統一関数を指定

@@ -45,3 +46,3 @@ // 空白とハイフン、アンダーバーを除去する

tt2.sub('coffeescript'); // 'coffeescript'を新規に追加(allowMinusがtrueなので-1が入る)
tt2.add('coffee-script'); // 曖昧さ統一レベルが3なので'coffeescript'に +1
console.log(tt2.add('coffee-script')); // 曖昧さ統一レベルが3なので'coffeescript'に +1、その'coffeescript'が返る
tt2.add('shell_script'); // 曖昧さ統一レベルが3なので'shellscript'に +1

@@ -48,0 +49,0 @@

@@ -1,160 +0,163 @@

/*jshint white:false, eqnull:true, immed:false, forin:false, globalstrict:true, node:true */
'use strict';
/*jshint white:false, eqnull:true, immed:false, boss:true, forin:false, node:true */
// Generated by CoffeeScript 1.6.2
var TinyTotalizer,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
// Generated by CoffeeScript 1.6.3
(function() {
var TinyTotalizer,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
TinyTotalizer = (function() {
var _calc, _fixNumber, _match, _standardize,
_this = this;
TinyTotalizer = (function() {
var _calc, _fixNumber, _match, _standardize,
_this = this;
_standardize = [
function(s) {
return s;
}, function(s) {
return s.toLowerCase();
}, function(s) {
return s.toLowerCase().replace(/\s/g, '');
}
];
_standardize = [
function(s) {
return s;
}, function(s) {
return s.toLowerCase();
}, function(s) {
return s.toLowerCase().replace(/\s/g, '');
}
];
function TinyTotalizer(opt, hash) {
var k, v, _base, _base1, _ref, _ref1, _ref2;
this.opt = opt != null ? opt : {};
this.hash = hash != null ? hash : {};
this.ranking = __bind(this.ranking, this);
this.sub = __bind(this.sub, this);
this.add = __bind(this.add, this);
if ((_ref = (_base = this.opt).allowMinus) == null) {
_base.allowMinus = true;
}
if ((_ref1 = (_base1 = this.opt).fuzzyLevel) == null) {
_base1.fuzzyLevel = 0;
}
if (this.opt.standardizer instanceof Function) {
this.opt.fuzzyLevel = _standardize.length;
_standardize.push(this.opt.standardizer);
}
_ref2 = this.hash;
for (k in _ref2) {
v = _ref2[k];
if (typeof v === 'number') {
if (!this.opt.allowMinus && v < 0) {
this.hash[k] = 0;
}
} else {
if (typeof v === 'string') {
v = v.trim();
if (/^\d+$/.test(v)) {
this.hash[k] = Number(v);
continue;
function TinyTotalizer(opt, hash) {
var k, v, _base, _base1, _base2, _ref;
this.opt = opt != null ? opt : {};
this.hash = hash != null ? hash : {};
this.ranking = __bind(this.ranking, this);
this.sub = __bind(this.sub, this);
this.add = __bind(this.add, this);
if ((_base = this.opt).allowMinus == null) {
_base.allowMinus = true;
}
if ((_base1 = this.opt).fuzzyLevel == null) {
_base1.fuzzyLevel = 0;
}
if ((_base2 = this.opt).calcReturnIsKey == null) {
_base2.calcReturnIsKey = false;
}
if (this.opt.standardizer instanceof Function) {
this.opt.fuzzyLevel = _standardize.length;
_standardize.push(this.opt.standardizer);
}
_ref = this.hash;
for (k in _ref) {
v = _ref[k];
if (typeof v === 'number') {
if (!this.opt.allowMinus && v < 0) {
this.hash[k] = 0;
}
} else {
if (typeof v === 'string') {
v = v.trim();
if (/^\d+$/.test(v)) {
this.hash[k] = Number(v);
continue;
}
}
delete this.hash[k];
}
delete this.hash[k];
}
}
}
_match = function(key) {
var hk, k, mk, _ref, _ref1;
_match = function(key) {
var hk, k, mk, _ref, _ref1;
if (this.hash.hasOwnProperty(key)) {
return key;
}
mk = _standardize[this.opt.fuzzyLevel](key);
hk = (_ref = (function() {
var _results;
_results = [];
for (k in this.hash) {
if (mk === _standardize[this.opt.fuzzyLevel](k)) {
_results.push(k);
}
}
return _results;
}).call(this)) != null ? _ref : [];
return (_ref1 = hk[0]) != null ? _ref1 : key;
};
if (this.hash.hasOwnProperty(key)) {
return key;
}
mk = _standardize[this.opt.fuzzyLevel](key);
hk = (_ref = (function() {
var _results;
_calc = function(key, n) {
var mKey, _base;
mKey = _match.call(this, key, this.opt.fuzzyLevel);
if ((_base = this.hash)[mKey] == null) {
_base[mKey] = 0;
}
this.hash[mKey] += n;
if (this.hash[mKey] < 0 && !this.opt.allowMinus) {
this.hash[mKey] = 0;
}
if (this.opt.calcReturnIsKey) {
return mKey;
} else {
return this.hash[mKey];
}
};
_results = [];
for (k in this.hash) {
if (mk === _standardize[this.opt.fuzzyLevel](k)) {
_results.push(k);
_fixNumber = function(n) {
if (typeof n === 'number') {
return n;
}
if (typeof n === 'string') {
if (/^\d+$/.test(n.trim())) {
return Number(n);
}
}
return _results;
}).call(this)) != null ? _ref : [];
return (_ref1 = hk[0]) != null ? _ref1 : key;
};
return 0;
};
_calc = function(key, n) {
var mKey, _base, _ref;
TinyTotalizer.prototype.add = function(key, n) {
if (n == null) {
n = 1;
}
return _calc.call(this, key, _fixNumber(n));
};
mKey = _match.call(this, key, this.opt.fuzzyLevel);
if ((_ref = (_base = this.hash)[mKey]) == null) {
_base[mKey] = 0;
}
this.hash[mKey] += n;
if (this.hash[mKey] < 0 && !this.opt.allowMinus) {
this.hash[mKey] = 0;
}
return this.hash[mKey];
};
TinyTotalizer.prototype.sub = function(key, n) {
if (n == null) {
n = 1;
}
return _calc.call(this, key, _fixNumber(n) * -1);
};
_fixNumber = function(n) {
if (typeof n === 'number') {
return n;
}
if (typeof n === 'string') {
if (/^\d+$/.test(n.trim())) {
return Number(n);
TinyTotalizer.prototype.ranking = function(opt) {
var ary, count, name, _ref;
if (opt == null) {
opt = {};
}
}
return 0;
};
ary = (function() {
var _ref, _results;
_ref = this.hash;
_results = [];
for (name in _ref) {
count = _ref[name];
if (!opt.border || count >= opt.border) {
_results.push({
name: name,
count: count
});
}
}
return _results;
}).call(this);
if (opt.asc) {
ary.sort(function(a, b) {
return a.count - b.count;
});
} else {
ary.sort(function(a, b) {
return b.count - a.count;
});
}
ary.length = Math.min(ary.length, (_ref = opt.topN) != null ? _ref : ary.length);
return ary;
};
TinyTotalizer.prototype.add = function(key, n) {
if (n == null) {
n = 1;
}
return _calc.call(this, key, _fixNumber(n));
};
return TinyTotalizer;
TinyTotalizer.prototype.sub = function(key, n) {
if (n == null) {
n = 1;
}
return _calc.call(this, key, _fixNumber(n) * -1);
};
}).call(this);
TinyTotalizer.prototype.ranking = function(opt) {
var ary, count, name, _ref;
module.exports = TinyTotalizer;
if (opt == null) {
opt = {};
}
ary = (function() {
var _ref, _results;
_ref = this.hash;
_results = [];
for (name in _ref) {
count = _ref[name];
if (!opt.border || count >= opt.border) {
_results.push({
name: name,
count: count
});
}
}
return _results;
}).call(this);
if (opt.asc) {
ary.sort(function(a, b) {
return a.count - b.count;
});
} else {
ary.sort(function(a, b) {
return b.count - a.count;
});
}
ary.length = Math.min(ary.length, (_ref = opt.topN) != null ? _ref : ary.length);
return ary;
};
return TinyTotalizer;
}).call(this);
module.exports = TinyTotalizer;
{
"name": "tiny-totalizer",
"version": "0.1.0",
"version": "0.1.1",
"description": "Simple and tiny totalisation module using hash",
"main": "index.js",
"scripts": {
"test": "vows --spec test/test.js"
"test": "vows --spec test/*.js"
},

@@ -9,0 +9,0 @@ "repository": {

@@ -75,2 +75,6 @@ # TinyTotalizer - 連想配列を使用したNode.js用簡易集計モジュール

* calcReturnIsKey (`true`/`false`, デフォルト: `false`)
`add`、`sub`メソッドの戻り値はデフォルト(`false`)ではそのキーの現在のカウント数ですが、このオプションを`true`にすると戻り値が統一後のキー名になります。今加算(減算)したキーがそのまま格納されたのか、既存のどこかのキーに統一化されて格納されたのかを判別したい時に使用します。
* standardizer (関数)

@@ -139,2 +143,6 @@

### 0.1.1 (2013-10-02)
* コンストラクタのオプションに`calcReturnIsKey`追加
### 0.1.0 (2013-03-20)

@@ -141,0 +149,0 @@

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