-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
131 lines (112 loc) · 3.01 KB
/
index.js
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
/* eslint-disable no-extra-boolean-cast */
"use strict";
// Require Internal Dependencies
const { canItBePrimitive, isPlainObject, toNullable, isValueIterable } = require("./src/utils");
const toNullableString = toNullable(toString);
const toNullableNumber = toNullable(toNumber);
const toNullableBoolean = toNullable((value) => Boolean(value));
/**
* @function toNumber
* @description convert any JavaScript value to a Number (if not possible it will throw a TypeError);
* @param {!any} value
* @returns {number}
*
* @throws {TypeError}
*/
function toNumber(value) {
if (typeof value === "bigint") {
return value;
}
const expectedNumberValue = Number.parseInt(value, 10);
if (Number.isNaN(expectedNumberValue)) {
throw new TypeError("value must be a valid number representation");
}
return expectedNumberValue;
}
/**
* @function toBigInt
* @param {!any} value
* @returns {bigint}
*/
function toBigInt(value) {
return typeof value === "bigint" ? value : BigInt(toNumber(value));
}
/**
* @function toString
* @param {!any} value
* @param {object} [options]
* @param {boolean} [options.allowEmptyString=true]
* @returns {string}
*
* @throws {TypeError}
*/
function toString(value, options) {
const localOptions = toPlainObject(options, true);
const allowEmptyString = toNullableBoolean(localOptions.allowEmptyString) ?? true;
const type = typeof value;
if (type === "symbol") {
return value.description;
}
if (type === "object" && !canItBePrimitive(value)) {
throw new TypeError("value must be a valid string representation");
}
const resultStr = String(value);
if (!allowEmptyString && resultStr.trimStart().length === 0) {
throw new TypeError("value can not be an empty string");
}
return resultStr;
}
/**
* @function toSymString
* @param {!any} value
* @returns {string|symbol}
*/
function toSymString(value) {
if (typeof value === "symbol") {
return value;
}
return toString(value);
}
/**
* @function toIterable
* @param {!any} value
* @returns {IterableIterator<any>}
*/
function toIterable(value) {
return isValueIterable(value) ? value : Array.from(value);
}
/**
* @function toPlainObject
* @param {!any} value
* @param {boolean} [handleNullAndUndefined=false]
* @returns {object}
*
* @throws {TypeError}
*/
function toPlainObject(value, handleNullAndUndefined = false) {
if (handleNullAndUndefined && (value === null || typeof value === "undefined")) {
return Object.create(null);
}
if (isValueIterable(value)) {
return Object.fromEntries(value);
}
// Note: this is a bad pattern to handle cross-realm objects!
if (!isPlainObject(value)) {
throw new TypeError("value must be an object");
}
return value;
}
module.exports = {
toNumber,
toBigInt,
toString,
toSymString,
toNullableString,
toNullableNumber,
toNullableBoolean,
toIterable,
toPlainObject,
utils: {
toNullable
}
};