some-common-functions-js 1.0.0
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/README.md +92 -0
- package/index.js +160 -0
- package/package.json +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# JavaScript Object Utilities & Field Validation Functions
|
|
2
|
+
|
|
3
|
+
Common functions used for working with JavaScript objects and validating field values.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. JavaScript Object Utilities
|
|
8
|
+
|
|
9
|
+
### `getPaths(anObject)`
|
|
10
|
+
Returns a string array of path/field names inside a JavaScript object.
|
|
11
|
+
** Example **
|
|
12
|
+
let client = {
|
|
13
|
+
name: "Jack",
|
|
14
|
+
surname: "Stober",
|
|
15
|
+
address: {
|
|
16
|
+
streetNum: "57",
|
|
17
|
+
streetName: "Tiger Drive",
|
|
18
|
+
suburb: "Lakeside Ext.1",
|
|
19
|
+
town: "Lakeside",
|
|
20
|
+
country {
|
|
21
|
+
name: "South Africa",
|
|
22
|
+
code: "za"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let paths = getPaths(client);
|
|
28
|
+
// ["name", "surname", "address.streetNum", "address.streetName", "address.suburb",
|
|
29
|
+
// "address.town", "address.country.name", "address.country.code"]
|
|
30
|
+
|
|
31
|
+
### `hasOnly(anObject, ...fields)`
|
|
32
|
+
Returns `true` if the object contains **only** some or all of the specified fields and no others.
|
|
33
|
+
** Examples **
|
|
34
|
+
let car = {
|
|
35
|
+
make: "Ford",
|
|
36
|
+
model: "Ranger",
|
|
37
|
+
year: "2015",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
let result = hasOnly(car, ["make", "model", "year"]);
|
|
41
|
+
// true, because car has *only all of the specified fields and no other fields.
|
|
42
|
+
|
|
43
|
+
result = hasOnly(car, ["make", "model", "year", "maxSpeed", "gvm"]);
|
|
44
|
+
// true, because car has *only some of the specified fields and no other fields.
|
|
45
|
+
|
|
46
|
+
result = hasOnly(car, ["maxSpeed", "gvm", "power"]);
|
|
47
|
+
// false, because car has fields other than the specified fields.
|
|
48
|
+
|
|
49
|
+
result = hasOnly(car, ["make", "model"]);
|
|
50
|
+
// false, because car has fields other than the specified fields.
|
|
51
|
+
|
|
52
|
+
### `hasAll(anObject, ...fields)`
|
|
53
|
+
Returns `true` if the object contains **all** the specified fields.
|
|
54
|
+
The object may contain additional fields.
|
|
55
|
+
** Example **
|
|
56
|
+
let car = {
|
|
57
|
+
make: "Ford",
|
|
58
|
+
model: "Ranger",
|
|
59
|
+
year: "2015",
|
|
60
|
+
power: "1000kW",
|
|
61
|
+
type: "pickup truck"
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
let result = hasAll(car, ["make", "model", "year"]);
|
|
65
|
+
// true, because car has all the specified fields.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## 2. Field Validation Functions
|
|
70
|
+
|
|
71
|
+
### `isValidUserName(userName)`
|
|
72
|
+
Returns `true` if a username is valid.
|
|
73
|
+
|
|
74
|
+
### `isValidName(name)`
|
|
75
|
+
Returns `true` if a personal name is valid.
|
|
76
|
+
|
|
77
|
+
### `isValidEmail(email)`
|
|
78
|
+
Returns `true` if an email address is valid.
|
|
79
|
+
|
|
80
|
+
### `isValidPhoneNum(num)`
|
|
81
|
+
Returns `true` if a phone number is valid.
|
|
82
|
+
|
|
83
|
+
### `isValidOrganisationName(name)`
|
|
84
|
+
Returns `true` if an organisation name is valid.
|
|
85
|
+
|
|
86
|
+
### `isValidPassword(password)`
|
|
87
|
+
Returns `true` if the password meets the required strength rules.
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/** File: ./backend/utilityFunctions/commonFunctions.js
|
|
2
|
+
* Description: Common functions to be put here.
|
|
3
|
+
* Date Dev Version Description
|
|
4
|
+
* 2024/09/01 ITA 1.00 Genesis.
|
|
5
|
+
* 2024/11/11 ITA 1.02 Added file header. Function getPaths to return a sorted array of paths.
|
|
6
|
+
* 2024/11/29 ITA 1.03 Function to be added to export soon after definition.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
function isValidUserName(userName) {
|
|
11
|
+
if (!userName) // The username must be provided.
|
|
12
|
+
return false;
|
|
13
|
+
|
|
14
|
+
const regEx = /^[a-zA-Z][a-zA-Z0-9_-]{2,50}$/;
|
|
15
|
+
return regEx.test(userName);
|
|
16
|
+
}
|
|
17
|
+
module.exports.isValidUserName = isValidUserName;
|
|
18
|
+
|
|
19
|
+
function isValidName(name) {
|
|
20
|
+
if (!name) // The name must be provided.
|
|
21
|
+
return false;
|
|
22
|
+
|
|
23
|
+
const regEx = /^[A-Za-z' -]{2,50}$/;
|
|
24
|
+
return regEx.test(name);
|
|
25
|
+
}
|
|
26
|
+
module.exports.isValidName = isValidName;
|
|
27
|
+
|
|
28
|
+
function isValidEmail(email) {
|
|
29
|
+
if (!email)
|
|
30
|
+
return false;
|
|
31
|
+
|
|
32
|
+
const regEx = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
|
33
|
+
return regEx.test(email);
|
|
34
|
+
}
|
|
35
|
+
module.exports.isValidEmail = isValidEmail;
|
|
36
|
+
|
|
37
|
+
function isValidPhoneNum(num) {
|
|
38
|
+
if (!num) // The number must be provided.
|
|
39
|
+
return false;
|
|
40
|
+
|
|
41
|
+
const regEx = /^[0-9]{10,15}$/g;
|
|
42
|
+
return regEx.test(num);
|
|
43
|
+
}
|
|
44
|
+
module.exports.isValidPhoneNum = isValidPhoneNum;
|
|
45
|
+
|
|
46
|
+
function isValidOrganisationName(name) {
|
|
47
|
+
if (!name) // The name must be provided.
|
|
48
|
+
return false;
|
|
49
|
+
|
|
50
|
+
const regEx = /^[a-zA-Z0-9.\-\(\) ]{2,}$/;
|
|
51
|
+
return regEx.test(name);
|
|
52
|
+
}
|
|
53
|
+
module.exports.isValidOrganisationName = isValidOrganisationName;
|
|
54
|
+
|
|
55
|
+
function isValidPassword(password) {
|
|
56
|
+
if (!password)
|
|
57
|
+
return false;
|
|
58
|
+
|
|
59
|
+
// Password must be at least 6 characters long
|
|
60
|
+
if (password.length < 6)
|
|
61
|
+
return false;
|
|
62
|
+
|
|
63
|
+
// Must contain at least 1 uppercase letter
|
|
64
|
+
if (/[A-Z]/.test(password) === false)
|
|
65
|
+
return false;
|
|
66
|
+
|
|
67
|
+
// Must contain at least 1 lowercase letter
|
|
68
|
+
if (/[a-z]/.test(password) === false)
|
|
69
|
+
return false;
|
|
70
|
+
|
|
71
|
+
// Must contain at least 1 number
|
|
72
|
+
if (/[0-9]/.test(password) === false)
|
|
73
|
+
return false;
|
|
74
|
+
|
|
75
|
+
// Must not contain white space characters
|
|
76
|
+
if (/[\s]/.test(password))
|
|
77
|
+
return false;
|
|
78
|
+
|
|
79
|
+
// Must contain atleast 1 symbol
|
|
80
|
+
if (/[\][!"#$%&'()*+,./:;<=>?@^\\_`{|}~-]/.test(password) === false)
|
|
81
|
+
return false;
|
|
82
|
+
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
module.exports.isValidPassword = isValidPassword;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Get the paths (fields) of the plain Javascript object.
|
|
89
|
+
* @param {object} anObject
|
|
90
|
+
* @returns a string array of paths.
|
|
91
|
+
*/
|
|
92
|
+
function getPaths(anObject) {
|
|
93
|
+
const paths = [];
|
|
94
|
+
if (!(Object.prototype.toString.call(anObject) === '[object Object]'))
|
|
95
|
+
return paths;
|
|
96
|
+
|
|
97
|
+
for (const path in anObject) {
|
|
98
|
+
const nestedPaths = getPaths(anObject[path]);
|
|
99
|
+
if (nestedPaths.length > 0) {
|
|
100
|
+
nestedPaths.forEach(nestedPath=> {
|
|
101
|
+
paths.push(path + '.' + nestedPath);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else
|
|
105
|
+
paths.push(path);
|
|
106
|
+
}
|
|
107
|
+
paths.sort();
|
|
108
|
+
return paths;
|
|
109
|
+
} // function getPaths()
|
|
110
|
+
module.exports.getPaths = getPaths;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Determine whether an object contains only some or all of the specified fields, and not any other fields.
|
|
114
|
+
* @param {*} anObject a Javascript object.
|
|
115
|
+
* @param {...string} fields one or more field names.
|
|
116
|
+
* @returns boolean.
|
|
117
|
+
*/
|
|
118
|
+
function hasOnly(anObject, ...fields) {
|
|
119
|
+
if (!fields || !fields.length)
|
|
120
|
+
throw new Error('fields must be specified');
|
|
121
|
+
|
|
122
|
+
const paths = getPaths(anObject);
|
|
123
|
+
let count = 0;
|
|
124
|
+
for (const index in paths) {
|
|
125
|
+
const path = paths[index];
|
|
126
|
+
|
|
127
|
+
if (!fields.includes(path))
|
|
128
|
+
return false;
|
|
129
|
+
else
|
|
130
|
+
count++;
|
|
131
|
+
} // for (const index in paths)
|
|
132
|
+
|
|
133
|
+
return (count > 0);
|
|
134
|
+
}
|
|
135
|
+
module.exports.hasOnly = hasOnly;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Determine whether an object contains all of the specified fields. It may have additional fields.
|
|
139
|
+
* @param {*} anObject a Javascript object.
|
|
140
|
+
* @param {...string} fields one or field names.
|
|
141
|
+
* @returns boolean.
|
|
142
|
+
*/
|
|
143
|
+
function hasAll(anObject, ...fields) {
|
|
144
|
+
if (!fields || !fields.length)
|
|
145
|
+
throw new Error('fields must be specified');
|
|
146
|
+
|
|
147
|
+
const paths = getPaths(anObject);
|
|
148
|
+
let count = 0;
|
|
149
|
+
for (const index in fields) {
|
|
150
|
+
const field = fields[index];
|
|
151
|
+
|
|
152
|
+
if (!paths.includes(field))
|
|
153
|
+
return false;
|
|
154
|
+
else
|
|
155
|
+
count++;
|
|
156
|
+
} // for (const index in paths)
|
|
157
|
+
|
|
158
|
+
return (count === fields.length);
|
|
159
|
+
}
|
|
160
|
+
module.exports.hasAll = hasAll;
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "some-common-functions-js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Common functions used with Javascript objects, and field validation functions.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/IsaiahTshabalala/common-functions"
|
|
8
|
+
},
|
|
9
|
+
"license": "ISC",
|
|
10
|
+
"author": "ITA",
|
|
11
|
+
"type": "commonjs",
|
|
12
|
+
"main": "index.js",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
15
|
+
}
|
|
16
|
+
}
|