...
 
Commits (2)
{
"name": "@creanet/js-db",
"version": "2.1.0",
"version": "2.1.1",
"description": "Used for communication between NodeJS and PostgreSQL",
"main": "index.js",
"scripts": {
......@@ -19,7 +19,8 @@
"author": "Milan Wikarski,Marcel Odumorek",
"license": "ISC",
"dependencies": {
"pg": "^7.8.2"
"pg": "^7.8.2",
"@creanet/js-type": "1.0.0"
},
"devDependencies": {
"dotenv": "^8.2.0"
......
const is = require('../utils/types');
const is = require('@creanet/js-type');
const ColumnsMixin = C =>
class ColumnsMixin extends C {
......
const is = require('../utils/types');
const is = require('@creanet/js-type');
const LimitMixin = C =>
class LimitMixin extends C {
......
const is = require('../utils/types');
const is = require('@creanet/js-type');
const OffsetMixin = C =>
class OffsetMixin extends C {
......
const is = require('../utils/types');
const is = require('@creanet/js-type');
const ValuesMixin = C =>
class ValuesMixin extends C {
......
const is = require('../utils/types');
const is = require('@creanet/js-type');
const WhereMixin = C =>
class WhereMixin extends C {
......
const snake2camel = obj => {
if (typeof obj === 'string') {
return obj
.split('_')
.map((word, i) =>
i === 0
? word.toLowerCase()
: word
.split('')
.map((char, i) =>
i === 0 ? char.toUpperCase() : char.toLowerCase()
)
.join('')
)
.join('');
} else {
Object.keys(obj).forEach(key => {
const camelCase = snake2camel(key);
if (key !== camelCase) {
obj[camelCase] = obj[key];
delete obj[key];
}
});
return obj;
}
};
exports.snake2camel = snake2camel;
/**
* Returns `true` if val is a string; `false` otherwise
*
* @param {*} val
* @returns {boolean}
*/
function isString(val) {
return typeof val == 'string';
}
/**
* Returns `true` if val is an array; `false` otherwise
*
* @param {*} val
* @returns {boolean}
*/
function isArray(val) {
return Array.isArray(val);
}
/**
* Returns `true` if val is numeric; `false` otherwise
*
* @param {*} val
* @returns {boolean}
*/
function isNumeric(val) {
return !isNaN(Number(val));
}
/**
* Returns `true` is val is instanceof prototype
*
* @param {*} val
* @param {*} prototype
* @returns {Boolean}
*/
function isInstance(val, prototype) {
return val instanceof prototype;
}
/**
* Returns `true` is val is Object; `false` otherwise
*
* @param {*} val
* @returns {Boolean}
*/
function isObject(val) {
return isInstance(val, Object);
}
const _is = {
string: isString,
array: isArray,
numeric: isNumeric,
instance: isInstance,
object: isObject
};
/**
* Checks if value is of one of valid types specified by `types` array.
* valid items in `types` are:
* - `{string}`: type name (eg. 'numeric') - checks type
* - `{Object}`: instance (eg. Regex) - checks instanceof
*
* @param {*} val
* @param {Array.<string|Object>} types
* @returns
*/
function is(val, types) {
if (!_is.array(types)) throw TypeError('Types is not an array');
for (let i = 0; i < types.length; i++) {
const type = types[i];
if (isString(type)) {
if (_is[type](val)) {
return true;
}
} else if (isObject(type)) {
if (isInstance(val, type)) {
return true;
}
}
}
return false;
}
is.string = isString;
is.array = isArray;
is.numeric = isNumeric;
is.instance = isInstance;
is.object = isObject;
module.exports = is;