-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
205 lines (187 loc) · 5.01 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import * as crypto from 'crypto';
import * as types from './types';
import * as util from 'util';
import { ObjectId, BSON } from 'bson';
import * as constants from './constants';
import { phone } from 'phone';
const _isNil = require('@ramda/isnil');
const _isEmpty = require('@ramda/isempty');
const writtenNumber = require('written-number');
/**
* It returns `true` if `x` is either `null` or `undefined`.
*/
export const isNil: types.ReturnBoolean = _isNil;
/**
* It returns `true` if `x` is `empty`.
*/
export const isEmpty: types.ReturnBoolean = _isEmpty;
/**
* It returns `true` if `x` is NOT `null` or `undefined`.
*/
export const isNotNil: types.ReturnBoolean = (x) => !isNil(x);
/**
* It returns `true` if `x` is NOT `empty`.
*/
export const isNotEmpty: types.ReturnBoolean = (x) => !isEmpty(x);
/**
* It returns `true` if `x` is `null` or `undefined` or `empty`.
*/
export const isNilOrEmpty: types.ReturnBoolean = (x) => {
return isNil(x) || isEmpty(x);
};
/**
* It returns `true` if `x` is NOT `null`, `undefined` and `empty`.
*/
export const isNotNilAndNotEmpty: types.ReturnBoolean = (x) => {
return !isNil(x) && !isEmpty(x);
};
/**
* Left pad string
* @param str String to pad
* @param char Character for padding
* @param len Length of final string, should be greater than string length
* @returns paddedString
*/
export const leftPad = (str: string, char: string, len: number): string => {
if (len < str.length) {
return str;
}
return char.repeat(len - str.length) + str;
};
/**
* Generate a string with random characters of a given length
* @example
* token(10)
* // returns 'a2377377c2'
*/
export const token = (length: number = 10): string => {
return crypto
.randomUUID()
.split('-')
.join('')
.slice(0, length)
.toLowerCase();
};
/**
* Generating a random number of a given length.
*/
export const randomNumber = (len: number = 4): number => {
const minValue = 10 ** (len - 1);
const maxValue = 10 ** len - 1;
return Math.floor(minValue + Math.random() * (maxValue - minValue));
};
/**
* Delay your code execution in milliseconds
*/
export const sleep = util.promisify(setTimeout);
/**
* Convert truthy string to boolean
*/
export const parseBoolean = (
val: string | null | undefined | boolean
): boolean => {
if (val === true || val === false) {
return val;
}
if (typeof val !== 'string' || val === null || val === undefined) {
return false;
}
const regex = new RegExp(/^true$/);
return regex.test(val.toLowerCase().trim());
};
/**
* Convert fields string array to a mongo projection object
* @param fieldsList A list of fields in a mongo record
* @returns projection object
* @example
* fieldsListToMongoProjection(['field1','field2','field3'])
* // returns { field1: 1, field2: 1, field3: 1 }
*/
export const fieldsListToMongoProjection = (
fieldsList: string[]
): types.MongoProjectionObject => {
return fieldsList.reduce((projectionObject, item) => {
return { ...projectionObject, [item]: 1 };
}, {});
};
/**
* Get pagination object for your list apis
* @param page Current page cursor
* @param pageSize
* @returns
*/
export const getPagination = (
page: number = 1,
pageSize: number = 10
): types.PaginationObject => {
const validPage = Math.floor(page);
const validPageSize = Math.floor(pageSize);
if (!validPage || !validPageSize) {
return {
offset: 0,
limit: 10,
isPageAndPageSize: false,
};
}
const offset = (validPage - 1) * validPageSize;
const limit = validPageSize;
return {
offset: offset,
limit: limit,
isPageAndPageSize: true,
};
};
/**
* Returns whether input is a valid mongo ObjectId
*/
export const isValidMongoObjectId = (x: string | number | ObjectId): boolean =>
ObjectId.isValid(x);
/**
* Convert input to mongo ObjectId
* @throws BSONError or BSONTypeError if input is invalid ObjectId
*/
export const convertToMongoObjectId = (
x: string | ObjectId
): ObjectId | BSON.BSONError => new ObjectId(x);
/**
* Converts a number to Capitalized indian words string
* @example
* numberToWords("10348")
* // returns 'Ten thousand three hundred and forty-eight'
* numberToWords("invalidNumber")
* // returns ''
*/
export const numberToIndianWords = (x: number | string): string => {
if (x === null || x === undefined) {
return '';
}
const str = writtenNumber(x, { lang: 'enIndian' });
return `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
};
/**
* Converts an Indian phone number to E164 format
* @example
* formatIndianPhoneNumber("1234567890")
* // returns '+911234567890'
* formatIndianPhoneNumber("91-12345-67890")
* // returns '+911234567890'
*/
export const formatIndianPhoneNumber = (
phoneNumber: string | number
): string => {
if (isNilOrEmpty(phoneNumber)) {
throw Error('Invalid Phone Number');
}
if (phoneNumber.toString().length < 10) {
throw Error('Invalid Phone Number');
}
const pVal = phone(phoneNumber.toString(), {
country: constants.CountryCode.INDIA,
validateMobilePrefix: false,
strictDetection: false,
});
if (!pVal.isValid) {
throw Error('Invalid Phone Number');
}
return pVal.phoneNumber;
};