@meteor-it/utils
Advanced tools
Comparing version 1.0.0 to 1.2.2
133
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const stream_1 = require("stream"); | ||
function firstUppercase(str) { | ||
@@ -94,14 +94,3 @@ return str.substr(0, 1).toUpperCase() + str.substr(1); | ||
function fixLength(string, length, insertPre = false, symbol = ' ') { | ||
if (string.length < length) { | ||
while (string.length < length) { | ||
string = insertPre ? symbol + string : string + symbol; | ||
} | ||
} | ||
else if (string.length > length) { | ||
try { | ||
string = string.match(insertPre ? new RegExp(`.*(.{${length}})`) : new RegExp(`(.{${length}})`))[0]; | ||
} | ||
catch (e) { } | ||
} | ||
return string; | ||
return insertPre ? string.padStart(length, symbol) : string.padEnd(length, symbol); | ||
} | ||
@@ -128,2 +117,120 @@ exports.fixLength = fixLength; | ||
exports.arrayKVObject = arrayKVObject; | ||
function sleep(time) { | ||
return new Promise((res) => { | ||
setTimeout(res, time); | ||
}); | ||
} | ||
exports.sleep = sleep; | ||
function asyncEach(iterable, cb) { | ||
let waitings = []; | ||
iterable.forEach(iter => { | ||
waitings.push(cb(iter)); | ||
}); | ||
return Promise.all(waitings); | ||
} | ||
exports.asyncEach = asyncEach; | ||
function cb2promise(cbFunction) { | ||
return (...args) => { | ||
return new Promise((res, rej) => { | ||
cbFunction(...args, (err, result) => { | ||
if (err) | ||
return rej(err); | ||
res(result); | ||
}); | ||
}); | ||
}; | ||
} | ||
exports.cb2promise = cb2promise; | ||
function hashCode(s) { | ||
let hash = 0; | ||
if (s.length === 0) | ||
return hash; | ||
for (let i = 0; i < s.length; i++) { | ||
let character = s.charCodeAt(i); | ||
hash = ((hash << 5) - hash) + character; | ||
hash = hash & hash; | ||
} | ||
return hash; | ||
} | ||
exports.hashCode = hashCode; | ||
function djb2Code(str) { | ||
let hash = 5381; | ||
for (let i = 0; i < str.length; i++) { | ||
let char = str.charCodeAt(i); | ||
hash = ((hash << 5) + hash) + char; | ||
} | ||
return hash; | ||
} | ||
exports.djb2Code = djb2Code; | ||
function sdbmCode(str) { | ||
let hash = 0; | ||
for (let i = 0; i < str.length; i++) { | ||
let char = str.charCodeAt(i); | ||
hash = char + (hash << 6) + (hash << 16) - hash; | ||
} | ||
return hash; | ||
} | ||
exports.sdbmCode = sdbmCode; | ||
function loseCode(str) { | ||
let hash = 0; | ||
for (let i = 0; i < str.length; i++) { | ||
hash += str.charCodeAt(i); | ||
} | ||
return hash; | ||
} | ||
exports.loseCode = loseCode; | ||
function encodeHtmlSpecials(str) { | ||
let i = str.length; | ||
let aRet = []; | ||
while (i--) { | ||
let iC = str[i].charCodeAt(); | ||
if (iC < 65 || iC > 127 || (iC > 90 && iC < 97)) { | ||
aRet[i] = '&#' + iC + ';'; | ||
} | ||
else { | ||
aRet[i] = str[i]; | ||
} | ||
} | ||
return aRet.join(''); | ||
} | ||
exports.encodeHtmlSpecials = encodeHtmlSpecials; | ||
function createReadStream(object, options = {}) { | ||
return new MultiStream(object, options); | ||
} | ||
exports.createReadStream = createReadStream; | ||
function readStream(stream) { | ||
return new Promise((res, rej) => { | ||
const bufs = []; | ||
stream.on('data', d => { | ||
bufs.push(d); | ||
}); | ||
stream.on('end', () => { | ||
let buf = Buffer.concat(bufs); | ||
res(buf); | ||
}); | ||
stream.on('error', rej); | ||
}); | ||
} | ||
exports.readStream = readStream; | ||
class MultiStream extends stream_1.Readable { | ||
constructor(object, options = {}) { | ||
if (object instanceof Buffer || typeof object === 'string') { | ||
super({ | ||
highWaterMark: options.highWaterMark, | ||
encoding: options.encoding | ||
}); | ||
} | ||
else { | ||
super({ | ||
objectMode: true | ||
}); | ||
} | ||
this._object = object; | ||
} | ||
_read() { | ||
this.push(this._object); | ||
this._object = null; | ||
} | ||
} | ||
exports.MultiStream = MultiStream; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@meteor-it/utils", | ||
"version": "1.0.0", | ||
"description": "Utils", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/Creeplays/utils.git" | ||
}, | ||
"keywords": [ | ||
"utils" | ||
], | ||
"author": "f6cf", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Creeplays/utils/issues" | ||
}, | ||
"homepage": "https://github.com/Creeplays/utils#readme" | ||
"name": "@meteor-it/utils", | ||
"version": "1.2.2", | ||
"description": "Many useful utils", | ||
"main": "index.js", | ||
"keywords": [ | ||
"meteor-it", | ||
"utils" | ||
], | ||
"author": "Yaroslaw Bolyukin <iam@f6cf.pw> (http://f6cf.pw/)", | ||
"license": "MIT" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
16788
6
268
1
1
2
1
1