UNPKG

3.08 kBMarkdownView Raw
1# environment
2
3[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
4[![current version](https://img.shields.io/npm/v/@robireton/environment)](https://www.npmjs.com/package/@robireton/environment)
5[![install size](https://packagephobia.com/badge?p=@robireton/environment)](https://packagephobia.com/result?p=@robireton/environment)
6
7*zero-dependency helpers for interacting with the process environment*
8
9Starting with v3.0.0, this is an ECMAScript module—stick with v2.x.x if you need a CommonJS module.
10
11---
12
13## install
14```sh
15$ npm install @robireton/environment
16```
17
18## usage
19```js
20import * as environment from '@robireton/environment'
21
22environment.parseBool('SOME_NAME')
23
24environment.parseInt('SOME_NAME')
25
26environment.parseInt('SOME_NAME', 1970)
27
28environment.parseFloat('SOME_NAME')
29
30environment.parseFloat('SOME_NAME', 2.71828)
31
32environment.parseList('SOME_NAME')
33
34```
35
36## methods
37
38### parseBool( `string` )
39
40#### arguments
41`string`: name of an environment variable
42
43#### returns
44`bool`: whether or not the environment variable is set to the string `true` (case insensitive)
45
46#### example
47```js
48environment.parseBool('SHELL')
49// => false
50```
51
52### parseInt( *name* : `string` [, *default* : `int` ] )
53
54#### arguments
55*name* : `string`: name of an environment variable
56
57*default* : `int`: a value to return if the name is not set or can’t be parsed
58
59#### returns
60`int`: result of parsing the value of *name*
61
62#### examples
63```js
64environment.parseInt('CLICOLOR')
65// => 1
66```
67
68```js
69environment.parseInt('SHELL', 1066)
70// => 1066
71```
72
73### parseInts( *name* : `string` [, *pattern* : `RegExp` or `string` ] )
74
75#### arguments
76*name* : `string`: name of an environment variable
77
78*pattern* : `regular expression` or `string` (defaults to `/[^0-9-]+/` — one or more non-digit/hyphen-minus characters) used to split the value of the environment variable into an array
79
80#### returns
81`[ int, … ]`: an array of integers, or an empty array if *name* isn’t set
82
83#### example
84```js
85environment.parseInts('127.0.0.1', '.')
86// => [ 127, 0, 0, 1 ]
87```
88
89### parseFloat( *name* : `string` [, *default* : `float` ] )
90
91#### arguments
92*name* : `string`: name of an environment variable
93
94*default* : `float`: a value to return if the name is not set or can’t be parsed
95
96#### returns
97`float`: result of parsing the value of *name*
98
99#### examples
100```js
101environment.parseFloat('TERM_PROGRAM_VERSION')
102// => 433
103```
104
105```js
106environment.parseFloat('SHELL', 3.1415)
107// => 3.1415
108```
109
110### parseList( *name* : `string` [, *pattern* : `RegExp` or `string` ] )
111
112#### arguments
113*name* : `string`: name of an environment variable
114
115*pattern* : `regular expression` or `string` (defaults to `/\W+/` — one or more non-word characters) used to split the value of the environment variable into an array
116
117#### returns
118`[ string, … ]`: an array of (non-empty) strings, or an empty array if *name* isn’t set
119
120#### example
121```js
122environment.parseList('PATH', ':')
123// => [ '/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin' ]
124```