Gathering detailed insights and metrics for @a-2-c-2-anpm/non-inventore-maiores
Gathering detailed insights and metrics for @a-2-c-2-anpm/non-inventore-maiores
Gathering detailed insights and metrics for @a-2-c-2-anpm/non-inventore-maiores
Gathering detailed insights and metrics for @a-2-c-2-anpm/non-inventore-maiores
npm install @a-2-c-2-anpm/non-inventore-maiores
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
3,048 Commits
1 Watchers
1 Branches
1 Contributors
Updated on May 20, 2025
Latest Version
1.0.0
Package Id
@a-2-c-2-anpm/non-inventore-maiores@1.0.0
Unpacked Size
36.28 kB
Size
10.77 kB
File Count
8
NPM Version
10.5.0
Node Version
20.12.2
Published on
May 04, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
30
typed-env
adds a layer over @humanwhocodes/env that allows you to convert environment variables into various JavaScript types.
The methods are in some cases identical and in some others very similar to those of @humanwhocodes/env
, but the way they work is the same. So, please, refer to the documentation of @humanwhocodes/env
to know more about.
The differences between typed-env
and @humanwhocodes/env
are basically four:
typed-env
is written in TypeScript following the Google TypeScript Style Guide. Have you found something not compliant with? Please provide a pull request.typed-env
allows you to convert environment variables into several different JavaScript types. The list of the supported and unsupported types and the way to convert is shown below.typed-env
checks the type of every input of every function via Runtypes: if there is a mismatch between the input expected type and the given one, an exception is thrown.typed-env
throws an exception if is not possible to convert the given input into the wanted type.1constructor (source_?: object) // same as @humanwhocodes/env 2static get version (): string // new: simply returns module version 3get (key_: string, toType_: string = 'string', defaultValue_?: unknown): any 4has (key_: string): boolean // same as @humanwhocodes/env 5first (keys_: string[], toType_: string = 'string', defaultValue_?: unknown): any 6require (key_: string, toType_: string = 'string'): any 7requireFirst (keys_: string[], toType_: string = 'string'): any 8get exists (): any // same as @humanwhocodes/env 9get required (): any // same as @humanwhocodes/env
This is the list of supported and unsupported JavaScript types according to the official JavaScript standard built-in objects
Type | Supported? | If not, why? |
---|---|---|
Infinity | ❌ | It's actually a number, so you can convert the string "Infinity" into number to get this. |
NaN | ❌ | It's actually a number, but it's considered as an invalid number value unless is passed as default value. Please read below to know more about. |
undefined | ❌ | It's the value assumed by environment variables if they are unset and no default value is provided, but you cannot convert variables into undefined. |
globalThis | ❌ | It's a special object defined by JavaScript runtime environment. Unable to covert into this. |
eval() | ❌ | It's a global function. Unable to covert into this. |
isFinite() | ❌ | It's a global function. Unable to covert into this. |
isNaN() | ❌ | It's a global function. Unable to covert into this. |
parseFloat() | ❌ | It's a global function. Unable to covert into this. |
parseInt() | ❌ | It's a global function. Unable to covert into this. |
encodeURI() | ❌ | It's a global function. Unable to covert into this. |
encodeURIComponent() | ❌ | It's a global function. Unable to covert into this. |
decodeURI() | ❌ | It's a global function. Unable to covert into this. |
decodeURIComponent() | ❌ | It's a global function. Unable to covert into this. |
escape() (deprecated) | ❌ | It's a global function. Unable to covert into this. |
unescape() (deprecated) | ❌ | It's a global function. Unable to covert into this. |
Object | ✅ | |
Function | ✅ | |
Boolean | ✅ | |
Symbol | ✅ | |
Error | ❌ | It's an error class, why convert a variable into error? |
AggregateError | ❌ | It's an error class, why convert a variable into error? |
EvalError | ❌ | It's an error class, why convert a variable into error? |
InternalError (non standard) | ❌ | It's an error class, why convert a variable into error? |
RangeError | ❌ | It's an error class, why convert a variable into error? |
ReferenceError | ❌ | It's an error class, why convert a variable into error? |
SyntaxError | ❌ | It's an error class, why convert a variable into error? |
TypeError | ❌ | It's an error class, why convert a variable into error? |
URIError | ❌ | It's an error class, why convert a variable into error? |
Number | ✅ | |
BigInt | ✅ | |
Math | ❌ | It's a class of static methods. Unable to covert into this. |
Date | ✅ | |
String | ✅ | |
RegExp | ✅ | |
Array | ✅ | |
Int8Array | ✅ | |
Uint8Array | ✅ | |
Uint8ClampedArray | ✅ | |
Int16Array | ✅ | |
Uint16Array | ✅ | |
Int32Array | ✅ | |
Uint32Array | ✅ | |
Float32Array | ✅ | |
Float64Array | ✅ | |
BigInt64Array | ✅ | |
BigUint64Array | ✅ | |
Map | ✅ | |
Set | ✅ | |
WeakMap | ❌ | It's required that keys must be Objects, how to do that? |
WeakSet | ❌ | It's required that keys must be Objects, how to do that? |
ArrayBuffer | ❌ | Not needed since you can simply convert environment variables into Typed Arrays. |
SharedArrayBuffer | ❌ | Not needed since you can simply convert environment variables into Typed Arrays. |
Atomics | ❌ | It's a class of static methods. Unable to covert into this. |
DataView | ❌ | Not needed since you can simply convert environment variables into Typed Arrays. |
JSON | ❌ | A JSON is actually a string, so you can set an environment variable to a JSON and simply covert it to string (or not convert it, if you prefer). |
Promise | ❌ | Not needed since you can simply convert environment variables into Functions. |
Generator | ❌ | Not needed since you can simply convert environment variables into Functions. |
GeneratorFunction | ❌ | Not needed since you can simply convert environment variables into Functions. |
AsyncFunction | ❌ | Not needed since you can simply convert environment variables into Functions. |
AsyncGenerator | ❌ | Not needed since you can simply convert environment variables into Functions. |
AsyncGeneratorFunction | ❌ | Not needed since you can simply convert environment variables into Functions. |
Reflect | ❌ | It's a class of static methods. Unable to covert into this. |
Proxy | ❌ | A Proxy - as name suggests - is a proxy to an existing Object, how to do that? |
Intl | ❌ | It's a class of various constructors and static methods. Unable to covert into this. |
Intl.Collator | ❌ | It's a class that offers methods for string comparison. Unable to covert into this. |
Intl.DateTimeFormat | ❌ | It's a class that offers methods for date formatting. Unable to covert into this. |
Intl.ListFormat | ❌ | It's a class that offers methods for array formatting. Unable to covert into this. |
Intl.NumberFormat | ❌ | It's a class that offers methods for number formatting. Unable to covert into this. |
Intl.PluralRules | ❌ | It's a class that offers methods for plural rules. Unable to covert into this. |
Intl.RelativeTimeFormat | ❌ | It's a class that offers methods for time formatting. Unable to covert into this. |
Intl.Locale | ❌ | It's a class that offers methods for easy manipulation of Unicode locales. Unable to covert into this. |
Do you thing something can be implemented? Please provide a pull request.
In the above examples method get()
will be used. Please refer to @humanwhocodes/env
documentation to know how it works and to know - as well - how first()
, require()
and requireFirst()
work.
Please note that in every example the argument toType_
will always appear in lower case. Anyway, this argument is case insensitive: if - for instance - you pass "array", or "ARRAY", or "Array", or "arRay", or any other combination of upper case and lower case letters that form the word "array", you'll always get the environment variable converted into Array (if the wanted environment variable can be converted into Array).
To convert into Object, JSON must be provided:
1TO_OBJECT="{\"key1\":\"val1\",\"key2\":42,\"key3\":{\"key4\":[1,2,3,4]}}" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myObject = env.get('TO_OBJECT', 'object') 6 7console.log(myObject.key2) // <-- this will print 42
To convert into Function, stringified function must be provided:
1TO_FUNCTION="x => x ** 2" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myFunction = env.get('TO_FUNCTION', 'function') 6const squared = myFunction(9) 7 8console.log(squared) // <-- this will print 81
For conversion into Functions the built-in object eval() is used, however eval() is known to be problematic since statement is directly executed and this exposes the application to security risks. So, please, use this conversion with caution.
Moreover, please note that only arrow functions are supported. Have you got a solution to support classic functions too? Please provide a pull request.
To convert into Boolean, either string "true" or string "false" must be provided:
1TO_BOOLEAN="false" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myBoolean = env.get('TO_BOOLEAN', 'boolean') 6 7console.log(myBoolean) // <-- this will print false
Only strings "true" and "false" are considered as valid convertible Boolean values. If you try to pass "0", "no", "1", "yes" and - in general - any other value that is neither "true" nor "false", an exception will be thrown.
To convert into Symbol, any string can be provided:
1TO_SYMBOL_1="example" TO_SYMBOL_2="{\"key1\":\"prop1\"}" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const mySymbol1 = env.get('TO_SYMBOL_1', 'symbol') 6const mySymbol2 = env.get('TO_SYMBOL_2', 'symbol') 7 8console.log(mySymbol1) // <-- this will print Symbol(example) 9console.log(mySymbol2) // <-- this will print Symbol({"key1":"prop1"})
To convert into Number, stringified number must be provided:
1TO_NUMBER_1="Infinity" TO_NUMBER_2="8.1" TO_NUMBER_3="42" TO_NUMBER_4="test" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myNumber1 = env.get('TO_NUMBER_1', 'number') 6const myNumber2 = env.get('TO_NUMBER_2', 'number') 7const myNumber3 = env.get('TO_NUMBER_3', 'number') 8 9console.log(myNumber1) // <-- this will print Infinity 10console.log(myNumber1) // <-- this will print 8.1 11console.log(myNumber3) // <-- this will print 42 12 13const myNumber4 = env.get('TO_NUMBER_4', 'number') // <-- this will throw an exception
Number() constructor doesn't throw an exception if the given value cannot be converted into number, it simply returns NaN because standard IEEE 754 requires it:
1console.log(Number('this is a string')) // <-- this will print NaN and no exception will be thrown
The fact is that NaN is actually... a number:
1console.log(typeof NaN) // <-- this will print number
So now the question is, how can I know if the conversion into NaN is intentional? The answer is I cannot.
That's why I decided to consider NaN as an invalid number value: if the conversion into Number results in NaN, an exception will be thrown.
However, NaN is considered valid if passed as default value:
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myVariable = env.get('MY_ENV_VAR', 'number', NaN) 6 7// What will happen here will depend on the following cases: 8// 1. MY_ENV_VAR is set: 9// 1.1 its converted value is not NaN --> myVariable will assume the converted value 10// 1.1 its converted value is NaN --> an exception will be thrown 11// 2. MY_ENV_VAR is unset --> myVariable will assume the value NaN
Why is NaN considered valid in here? Because - as mentioned - NaN is a number and the default value is decided by you. In other words, the choice to assign NaN to a variable, in this case, is per sure intentional and there is no reason to me to throw an exception.
To convert into BigInt, stringified integer must be provided:
1TO_BIG_INT="100" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myBigInt = env.get('TO_BIG_INT', 'bigint') 6 7console.log(myBigInt) // <-- this will print 100n
To convert into Date, either a stringified number or a date string must be provided:
1TO_DATE_1="946681200000" TO_DATE_2="2000-01-31T23:00:00.000Z" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myDate1 = env.get('TO_DATE_1', 'date') 6const myDate2 = env.get('TO_DATE_2', 'date') 7 8console.log(myDate1.toISOString()) // <-- this will print 1999-12-31T23:00:00.000Z (ISO String of January 1st 2000) 9console.log(myDate2.getTime()) // <-- this will print 949359600000 (Timestamp of February 1st 2000)
To convert into String, any string can be provided (this actually means to not convert the environment variable):
1TO_STRING_1="example" TO_STRING_2="test" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myString1 = env.get('TO_STRING_1', 'string') 6const myString2 = env.get('TO_STRING_2') // please note that default value for 7 // argument ToType_ is "string": if 8 // you want to convert an environment 9 // variable into a string, you can 10 // omit it 11 12console.log(myString1) // <-- this will print example 13console.log(myString1) // <-- this will print test
To convert into RegExp, stringified regular expressions must be provided:
1TO_REG_EXP="(?:10|100|1000)" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myRegExp = env.get('TO_REG_EXP', 'regexp') 6 7console.log(myRegExp) // <-- this will print /(?:10|100|1000)/
To convert into Array, stringified array must be provided:
1TO_ARRAY="[1,2,3,4]" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myArray = env.get('TO_ARRAY', 'array') 6 7console.log(myArray[2]) // <-- this will print 3
To convert into Typed Arrays, stringified array of numbers must be provided (Float64Array will be used in the example, but all other cases are equivalent):
1TO_FLOAT_64_ARRAY="[11.99,2,3.0002,4.2]" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myFloat64Array = env.get('TO_FLOAT_64_ARRAY', 'float64array') 6 7console.log(myFloat64Array[1]) // <-- this will print 11.99
Typed Arrays are serializable objects, however this serialization cannot be used to construct them:
1const myFloat64ArrayToString = myFloat64Array.toString() 2const JSONStringifiedMyFloat64Array = JSON.stringify(myFloat64Array) 3console.log(myFloat64ArrayToString) // <-- this will print 11.99,2,3.0002,4.2 4console.log(JSONStringifiedMyFloat64Array) // <-- this will print {"0":11.99,"1":2,"2":3.0002,"3":4.2}
None of the two aforementioned stringifications can be used to construct a Typed Array:
1const mySecondFloat64Array = new Float64Array(myFloat64ArrayToString) 2const myThirdFloat64Array = new Float64Array(JSONStringifiedMyFloat64Array) 3console.log(JSON.stringify(mySecondFloat64Array)) // <-- this will print {} 4console.log(JSON.stringify(myThirdFloat64Array)) // <-- this will print {}
That's why I made the choice of use stringified array of numbers to constructor Typed Arrays.
Please note that after the conversion into Array and before the construction of a Typed Array, is checked that all elements of the array are numbers. If not, an exception is thrown:
1TO_INT_16_ARRAY="[40,\"foo\"]" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myInt16Array = env.get('TO_INT_16_ARRAY', 'int16array') 6// the above line will throw an exception: "foo" that is the second element of 7// the provided array is not a number
To convert into Map, JSON must be provided:
1TO_MAP="{\"hello\":\"world\"}" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myMap = env.get('TO_MAP', 'map') 6 7console.log(myMap.get('hello')) // <-- this will print world
Maps are not serializable objects:
1const myMap = new Map([['key1', 'val1'],['key2','val2']]) 2console.log(myMap.toString()) // <-- this will print [object Map] 3console.log(JSON.stringify(myMap)) // <-- this will print {}
But since Map() constructor requires array of arrays, it's natural to use Objects - in conjunction with method Object.entries() - to construct Maps. Any kind of Object can be provided, Arrays too.
To convert into Set, stringified array must be provided:
1TO_SET="[\"entry1\",\"entry2\",\"entry3\"]" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const mySet = env.get('TO_SET', 'set') 6 7console.log(mySet.has('entry2')) // <-- this will print true 8console.log(mySet.has('entryX')) // <-- this will print false
Sets are not serializable object, as well as Maps:
1const mySet = new Set(['val1','val2','val3']) 2console.log(mySet.toString()) // <-- this will print [object Set] 3console.log(JSON.stringify(mySet)) // <-- this will print {}
But Set() constructor requires a "simple array" and that's why an array is required to construct Sets.
Default values must comply with the wanted conversion, otherwise an exception is thrown:
1TO_ARRAY="[1,2,3,4]" TO_FUNCTION="x => x ** 2" TO_NUMBER="10" node my_script.js
1const { TypedEnv } = require('@a-2-c-2-anpm/non-inventore-maiores') 2 3const env = new TypedEnv() 4 5const myArray = env.get('TO_ARRAY', 'array', []) 6// myArray will assume [1,2,3,4] as value because TO_ARRAY is set and it's 7// convertable to Array 8const myFunction = env.get('TO_FUNCTION', 'function', () => {}) 9// myFunction will assume x => x ** 2 as value because TO_FUNCTION is set and 10// it's convertable to Function 11const myNumber = env.get('TO_NUMBER', 'number', Infinity) 12// myNumber will assume 10 as value because TO_NUMBER is set and it's 13// convertable to Number 14const myObject = env.get('TO_OBJECT', 'object', {}) 15// myObject will assume {} as value because TO_OBJECT is not set 16 17const myWrongConversion = env.get('TO_FUNCTION', 'function', { key1: 'value1' }) 18// The above line will throw an exception because { key1: 'value1' } is not a 19// function. It doesn't matter if the environment variable is set or not: the 20// default value always must comply with the wanted conversion
These are getter methods and no argument can be passed. In other words, these methods work exactly as the ones of @humanwhocodes/env
and are the only two methods that doesn't allow the conversion of environment variables: they always return environment variables as strings.
Have you got a solution for this? Please provide a pull request.
No vulnerabilities found.
No security vulnerabilities found.