Skip to content

v8.x / TypeScript rewrite #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
resolve merge conflicts with master
  • Loading branch information
evanshortiss committed Dec 27, 2022
commit 55e81a634ba0595107f16232050229157e0ce0dc
6 changes: 6 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ A complete listing of the `env-var` API.
* [asArray()](#asarraydelimiter-string)
* [asBool()](#asbool)
* [asBoolStrict()](#asboolstrict)
* [asEmailString()](#asemailstring)
* [asEnum()](#asenumvalidvalues-string)
* [asFloat()](#asfloat)
* [asFloatNegative()](#asfloatnegative)
Expand Down Expand Up @@ -195,6 +196,11 @@ Attempt to parse the variable to a Boolean. Throws an exception if parsing
fails. The var must be set to either "true" or "false" (upper or lowercase) to
succeed.

#### asEmailString()

Read the variable and validate that it's an email address. Throws an exception
if the value is not an email address.

#### asEnum(validValues: string[])

Converts the value to a string, and matches against the list of valid values.
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 7.2.0 (01/09/2022)
* Add `asEmailString()` accessor (#146)

## 7.1.1 (28/10/2021)
* Fix duplicate identifier error for TypeScript builds (#151)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ You can use `env-var` in both JavaScript and TypeScript!
```js
const env = require('env-var');

// Import syntax is also supported
// import * as env from 'env-var'
// Or using module import syntax:
// import env from 'env-var'

const PASSWORD = env.get('DB_PASSWORD')
// Throw an error if the DB_PASSWORD variable is not set (optional)
Expand Down
13 changes: 12 additions & 1 deletion lib/accessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export interface Acccessors<T> {
asBool: (s: string) => boolean|T
asBoolStrict: (s: string) => boolean|T
asEnum: <V extends string>(s: V, validValues: Readonly<V[]>|V[]) => V|T
asArray: (s: string, delimiter?: string) => string[]|T
asArray: (s: string, delimiter?: string) => string[]|T,
asEmailString: (s: string) => string|T
}

export const accessors: Acccessors<never> = {
Expand All @@ -38,6 +39,16 @@ export const accessors: Acccessors<never> = {
return s
},

asEmailString (s) {
const EMAIL_REGEX = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\u0001-\u0008\u000b\u000c\u000e-\u001f\u0021\u0023-\u005b\u005d-\u007f]|\\[\u0001-\u0009\u000b\u000c\u000e-\u007f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\u0001-\u0008\u000b\u000c\u000e-\u001f\u0021-\u005a\u0053-\u007f]|\\[\u0001-\u0009\u000b\u000c\u000e-\u007f])+)\])$/

if (!EMAIL_REGEX.test(s)) {
throw new Error('should be a valid email address')
}

return s
},

asBool (s) {
const val = s.toLowerCase()

Expand Down
4 changes: 4 additions & 0 deletions lib/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,8 @@ export class Variable<T = undefined> {
public asUrlString() {
return this.getValue(accessors.asUrlString)
}

public asEmailString() {
return this.getValue(accessors.asEmailString)
}
}
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('env-var', function () {
ARRAY_WITH_DELIMITER_PREFIX: ',value',
DASH_ARRAY: '1-2-3',
URL: 'http://google.com/',
EMAIL: 'email@example.com',
ENUM: 'VALID',
EMPTY_STRING: '',
EMPTY_STRING_WITH_WHITESPACE: ' '
Expand Down Expand Up @@ -597,6 +598,21 @@ describe('env-var', function () {
})
})

describe('#asEmailString', function () {
it('should return an email address', function () {
expect(mod.get('EMAIL').asEmailString()).to.be.a('string')
expect(mod.get('EMAIL').asEmailString()).to.equal(TEST_VARS.EMAIL)
})

it('should throw due to a bad email address', function () {
process.env.EMAIL = '.invalid@example.com'

expect(() => {
mod.get('EMAIL').asEmailString()
}).to.throw('env-var: "EMAIL" should be a valid email address')
})
})

describe('#example', () => {
let fromMod

Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy