not-node 6.2.26 → 6.2.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/common.js +6 -2
- package/test/common.js +186 -144
package/package.json
CHANGED
package/src/common.js
CHANGED
|
@@ -76,13 +76,17 @@ module.exports.getTodayDate = () => {
|
|
|
76
76
|
/**
|
|
77
77
|
* Returns true if object has field of name
|
|
78
78
|
* @param {object} obj some object
|
|
79
|
-
* @param {string} name field name
|
|
79
|
+
* @param {string|Array<string>} name field name
|
|
80
80
|
* @return {boolean} if object contains field with name
|
|
81
81
|
**/
|
|
82
82
|
const objHas = (obj, name) => {
|
|
83
83
|
if (typeof obj === "undefined") return false;
|
|
84
84
|
if (obj === null) return false;
|
|
85
|
-
|
|
85
|
+
if (Array.isArray(name)) {
|
|
86
|
+
return name.every((itm) => typeof itm === "string" && objHas(obj, itm));
|
|
87
|
+
} else {
|
|
88
|
+
return Object.prototype.hasOwnProperty.call(obj, name);
|
|
89
|
+
}
|
|
86
90
|
};
|
|
87
91
|
module.exports.objHas = objHas;
|
|
88
92
|
|
package/test/common.js
CHANGED
|
@@ -1,151 +1,193 @@
|
|
|
1
|
-
const expect = require(
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
describe(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
describe('firstLetterToUpper', function() {
|
|
15
|
-
it('`some error` -> `Some error`', function() {
|
|
16
|
-
expect(Common.firstLetterToUpper('some error')).to.be.equal('Some error');
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
let testie = 'Иероним Босх';
|
|
21
|
-
describe(`validateObjectId, build in validator failed on ${testie}`, function() {
|
|
22
|
-
|
|
23
|
-
it(`Mongoose.Types.ObjectId.isValid('${testie}') -> true`, function(){
|
|
24
|
-
expect(ObjectId.isValid(testie)).to.be.ok;
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it(`validateObjectId(${testie}) -> false`, function() {
|
|
28
|
-
expect(Common.validateObjectId(testie)).to.be.not.ok;
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('validateObjectId(`5af96abbce4adb46c5202ed3`) -> true', function() {
|
|
32
|
-
expect(Common.validateObjectId('5af96abbce4adb46c5202ed3')).to.be.ok;
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('validateObjectId(undefined) -> false', function() {
|
|
36
|
-
expect(Common.validateObjectId(undefined)).to.be.false;
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
describe('compareObjectIds', function() {
|
|
41
|
-
it('null and null -> false', function() {
|
|
42
|
-
expect(Common.compareObjectIds(null, null)).to.be.false;
|
|
43
|
-
});
|
|
44
|
-
it('1 and 1 -> false', function() {
|
|
45
|
-
expect(Common.compareObjectIds(1, 1)).to.be.false;
|
|
46
|
-
});
|
|
47
|
-
it('"1" and 1 -> false', function() {
|
|
48
|
-
expect(Common.compareObjectIds("1", 1)).to.be.false;
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
describe('getTodayDate', ()=>{
|
|
54
|
-
it('today', ()=>{
|
|
55
|
-
const res = Common.getTodayDate();
|
|
56
|
-
expect(typeof res).to.be.equal('number');
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
//
|
|
60
|
-
describe('executeObjectFunction', ()=>{
|
|
61
|
-
it('promise', async ()=>{
|
|
62
|
-
const obj = {
|
|
63
|
-
async method(...params){
|
|
64
|
-
return 'apple '+params.join('.');
|
|
65
|
-
}
|
|
66
|
-
}, name = 'method', params = [1,2,3,true];
|
|
67
|
-
const res = await Common.executeObjectFunction(obj, name, params);
|
|
68
|
-
expect(res).to.be.equal('apple 1.2.3.true');
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('function', async ()=>{
|
|
72
|
-
const obj = {
|
|
73
|
-
method(...params){
|
|
74
|
-
return 'apple '+params.join('.');
|
|
75
|
-
}
|
|
76
|
-
}, name = 'method', params = [1,2,3,true];
|
|
77
|
-
const res = await Common.executeObjectFunction(obj, name, params);
|
|
78
|
-
expect(res).to.be.equal('apple 1.2.3.true');
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('!obj', async ()=>{
|
|
82
|
-
const obj = null, name = 'method', params = [1,2,3,true];
|
|
83
|
-
const res = await Common.executeObjectFunction(obj, name, params);
|
|
84
|
-
expect(res).to.be.undefined;
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
describe('mapBind', function() {
|
|
90
|
-
it('to is undefined, exception throwned', function(done) {
|
|
91
|
-
let to = undefined;
|
|
92
|
-
try {
|
|
93
|
-
Common.mapBind({getModel(){}}, to, ['getModel']);
|
|
94
|
-
console.log(to);
|
|
95
|
-
done(new Error('should throw'))
|
|
96
|
-
} catch (e) {
|
|
97
|
-
expect(e).to.be.instanceof(Error);
|
|
98
|
-
done()
|
|
99
|
-
}
|
|
1
|
+
const expect = require("chai").expect,
|
|
2
|
+
mongoose = require("mongoose"),
|
|
3
|
+
path = require("path"),
|
|
4
|
+
ObjectId = mongoose.Types.ObjectId,
|
|
5
|
+
Common = require("../src/common");
|
|
6
|
+
|
|
7
|
+
describe("Common", function () {
|
|
8
|
+
describe("firstLetterToLower", function () {
|
|
9
|
+
it("`Some error` -> `some error`", function () {
|
|
10
|
+
expect(Common.firstLetterToLower("Some error")).to.be.equal(
|
|
11
|
+
"some error"
|
|
12
|
+
);
|
|
13
|
+
});
|
|
100
14
|
});
|
|
101
15
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
16
|
+
describe("firstLetterToUpper", function () {
|
|
17
|
+
it("`some error` -> `Some error`", function () {
|
|
18
|
+
expect(Common.firstLetterToUpper("some error")).to.be.equal(
|
|
19
|
+
"Some error"
|
|
20
|
+
);
|
|
21
|
+
});
|
|
106
22
|
});
|
|
107
23
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
24
|
+
let testie = "Иероним Босх";
|
|
25
|
+
describe(`validateObjectId, build in validator failed on ${testie}`, function () {
|
|
26
|
+
it(`Mongoose.Types.ObjectId.isValid('${testie}') -> true`, function () {
|
|
27
|
+
expect(ObjectId.isValid(testie)).to.be.ok;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it(`validateObjectId(${testie}) -> false`, function () {
|
|
31
|
+
expect(Common.validateObjectId(testie)).to.be.not.ok;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("validateObjectId(`5af96abbce4adb46c5202ed3`) -> true", function () {
|
|
35
|
+
expect(Common.validateObjectId("5af96abbce4adb46c5202ed3")).to.be
|
|
36
|
+
.ok;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("validateObjectId(undefined) -> false", function () {
|
|
40
|
+
expect(Common.validateObjectId(undefined)).to.be.false;
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("compareObjectIds", function () {
|
|
45
|
+
it("null and null -> false", function () {
|
|
46
|
+
expect(Common.compareObjectIds(null, null)).to.be.false;
|
|
47
|
+
});
|
|
48
|
+
it("1 and 1 -> false", function () {
|
|
49
|
+
expect(Common.compareObjectIds(1, 1)).to.be.false;
|
|
50
|
+
});
|
|
51
|
+
it('"1" and 1 -> false', function () {
|
|
52
|
+
expect(Common.compareObjectIds("1", 1)).to.be.false;
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("getTodayDate", () => {
|
|
57
|
+
it("today", () => {
|
|
58
|
+
const res = Common.getTodayDate();
|
|
59
|
+
expect(typeof res).to.be.equal("number");
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
//
|
|
63
|
+
describe("executeObjectFunction", () => {
|
|
64
|
+
it("promise", async () => {
|
|
65
|
+
const obj = {
|
|
66
|
+
async method(...params) {
|
|
67
|
+
return "apple " + params.join(".");
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
name = "method",
|
|
71
|
+
params = [1, 2, 3, true];
|
|
72
|
+
const res = await Common.executeObjectFunction(obj, name, params);
|
|
73
|
+
expect(res).to.be.equal("apple 1.2.3.true");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("function", async () => {
|
|
77
|
+
const obj = {
|
|
78
|
+
method(...params) {
|
|
79
|
+
return "apple " + params.join(".");
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
name = "method",
|
|
83
|
+
params = [1, 2, 3, true];
|
|
84
|
+
const res = await Common.executeObjectFunction(obj, name, params);
|
|
85
|
+
expect(res).to.be.equal("apple 1.2.3.true");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("!obj", async () => {
|
|
89
|
+
const obj = null,
|
|
90
|
+
name = "method",
|
|
91
|
+
params = [1, 2, 3, true];
|
|
92
|
+
const res = await Common.executeObjectFunction(obj, name, params);
|
|
93
|
+
expect(res).to.be.undefined;
|
|
94
|
+
});
|
|
112
95
|
});
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
describe('tryFile', function() {
|
|
117
|
-
const pathToExistingFile = path.join(__dirname, 'testies/module/fields/collection.js');
|
|
118
|
-
const pathToAbsentFile = path.join(__dirname, 'testies/module/fields/collection.ejs');
|
|
119
|
-
const pathToDirectory = path.join(__dirname, 'testies/module/fields/empty');
|
|
120
|
-
|
|
121
|
-
it('file exists, type file', function() {
|
|
122
|
-
const res = Common.tryFile(pathToExistingFile);
|
|
123
|
-
expect(res).to.be.equal(true);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('file doesnt exist', function() {
|
|
127
|
-
const res = Common.tryFile(pathToAbsentFile);
|
|
128
|
-
expect(res).to.be.equal(false);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('directory', function() {
|
|
132
|
-
const res = Common.tryFile(pathToDirectory);
|
|
133
|
-
expect(res).to.be.equal(false);
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
describe('copyObj', function () {
|
|
138
|
-
it('empty', function () {
|
|
139
|
-
let rule = {};
|
|
140
|
-
expect(Common.copyObj(rule)).to.be.deep.equal({});
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it('not empty, then modification of new rule is not changes original', function () {
|
|
144
|
-
let rule = {some: ['data']};
|
|
145
|
-
let copyOfRule = Common.copyObj(rule);
|
|
146
|
-
delete copyOfRule.some;
|
|
147
|
-
expect(rule).to.be.deep.equal({some: ['data']});
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
96
|
|
|
97
|
+
describe("mapBind", function () {
|
|
98
|
+
it("to is undefined, exception throwned", function (done) {
|
|
99
|
+
let to = undefined;
|
|
100
|
+
try {
|
|
101
|
+
Common.mapBind({ getModel() {} }, to, ["getModel"]);
|
|
102
|
+
console.log(to);
|
|
103
|
+
done(new Error("should throw"));
|
|
104
|
+
} catch (e) {
|
|
105
|
+
expect(e).to.be.instanceof(Error);
|
|
106
|
+
done();
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("list is empty", function () {
|
|
111
|
+
const to = {};
|
|
112
|
+
Common.mapBind({}, to, []);
|
|
113
|
+
expect(to).to.be.deep.equal({});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("list item is not pointing to function", function () {
|
|
117
|
+
const to = {};
|
|
118
|
+
Common.mapBind({}, to, ["vasqa de gamma"]);
|
|
119
|
+
expect(to).to.be.deep.equal({});
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("tryFile", function () {
|
|
124
|
+
const pathToExistingFile = path.join(
|
|
125
|
+
__dirname,
|
|
126
|
+
"testies/module/fields/collection.js"
|
|
127
|
+
);
|
|
128
|
+
const pathToAbsentFile = path.join(
|
|
129
|
+
__dirname,
|
|
130
|
+
"testies/module/fields/collection.ejs"
|
|
131
|
+
);
|
|
132
|
+
const pathToDirectory = path.join(
|
|
133
|
+
__dirname,
|
|
134
|
+
"testies/module/fields/empty"
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
it("file exists, type file", function () {
|
|
138
|
+
const res = Common.tryFile(pathToExistingFile);
|
|
139
|
+
expect(res).to.be.equal(true);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("file doesnt exist", function () {
|
|
143
|
+
const res = Common.tryFile(pathToAbsentFile);
|
|
144
|
+
expect(res).to.be.equal(false);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("directory", function () {
|
|
148
|
+
const res = Common.tryFile(pathToDirectory);
|
|
149
|
+
expect(res).to.be.equal(false);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("copyObj", function () {
|
|
154
|
+
it("empty", function () {
|
|
155
|
+
let rule = {};
|
|
156
|
+
expect(Common.copyObj(rule)).to.be.deep.equal({});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("not empty, then modification of new rule is not changes original", function () {
|
|
160
|
+
let rule = { some: ["data"] };
|
|
161
|
+
let copyOfRule = Common.copyObj(rule);
|
|
162
|
+
delete copyOfRule.some;
|
|
163
|
+
expect(rule).to.be.deep.equal({ some: ["data"] });
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe("objHas", () => {
|
|
168
|
+
it("one field, exists", () => {
|
|
169
|
+
const obj = { field: 1 };
|
|
170
|
+
expect(Common.objHas(obj, "field")).to.be.true;
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("one field, dont exists", () => {
|
|
174
|
+
const obj = { field: 1 };
|
|
175
|
+
expect(Common.objHas(obj, "field2")).to.be.false;
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("few fields, one dont exists", () => {
|
|
179
|
+
const obj = { field: 1, goo: true, joe: "foo" };
|
|
180
|
+
expect(Common.objHas(obj, ["field", "goo", "laste"])).to.be.false;
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("few fields, all exists", () => {
|
|
184
|
+
const obj = { field: 1, goo: true, joe: "foo" };
|
|
185
|
+
expect(Common.objHas(obj, ["field", "goo", "joe"])).to.be.true;
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("few fields, some names is not string", () => {
|
|
189
|
+
const obj = { field: 1, goo: true, joe: "foo" };
|
|
190
|
+
expect(Common.objHas(obj, [1, "goo", undefined])).to.be.false;
|
|
191
|
+
});
|
|
192
|
+
});
|
|
151
193
|
});
|