-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3723aa9
commit a1e881f
Showing
8 changed files
with
131 additions
and
92 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,41 @@ | ||
# About | ||
# Tentang | ||
|
||
A no-dependency `.env` to plain JS object converter. You can also convert object to env and write it to a file. | ||
Pengubah `env` ke obyek JavaScript. | ||
|
||
## Motivation | ||
## Instalasi | ||
|
||
There has been some env to object converter in NPM registry, but none has satisfied me when I used it | ||
``` | ||
pnpm add envobject | ||
``` | ||
|
||
## Penggunaan | ||
|
||
Pakai string berformat env langsung: | ||
|
||
``` | ||
import { EnvDariString } from './induk.js' | ||
const stringEnv = ` | ||
PORT=8765 | ||
KEY=878yfjhdjhfd8 | ||
SIAP=yes | ||
` | ||
const hasil = (new EnvDariString(stringEnv)).keObyek() | ||
console.log(hasil) | ||
``` | ||
|
||
Pakai file env (metode `keObyek` bersifat **asingkron**): | ||
|
||
``` | ||
import { EnvDariFile } from './induk.js' | ||
const hasil = await (new EnvDariFile('./lokasi/file/.env')).keObyek() | ||
console.log(hasil) | ||
``` | ||
|
||
## Lisensi | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { readFile } from 'node:fs/promises' | ||
|
||
function mengandungSamaDengan(line) { | ||
return line.includes('=') | ||
} | ||
|
||
function throwNonString(input) { | ||
const tipeInput = typeof input | ||
|
||
if (tipeInput != 'string') { | ||
throw new TypeError(`Inputmu bukan string, tapi ${tipeInput}`) | ||
} | ||
|
||
return | ||
} | ||
|
||
function jadikanObyek(input) { | ||
// hapus spasi dan tab di awal, di akhir, dan dimanapun kecuali karakter baris baru | ||
// pemisahan berdasarkan baris | ||
const bahan = input | ||
.trim() | ||
.replace(/ +?/g, '') | ||
.split('\n') // step 1 | ||
|
||
if (!bahan.every(mengandungSamaDengan)) { | ||
throw new TypeError('String tidak berformat env. Coba lihat lagi.') | ||
} | ||
|
||
// pemisahan berdasarkan karakter = | ||
const bahan2 = bahan.map((item) => { | ||
return item.split('=') | ||
}) | ||
|
||
return Object.fromEntries(bahan2) | ||
} | ||
|
||
class EnvDariString { | ||
constructor(input) { | ||
throwNonString(input) | ||
|
||
this.input = input | ||
} | ||
|
||
keObyek() { | ||
return jadikanObyek(this.input) | ||
} | ||
} | ||
|
||
class EnvDariFile { | ||
constructor(input) { | ||
throwNonString(input) | ||
|
||
this.input = input | ||
} | ||
|
||
async keObyek() { | ||
const isiFile = (await readFile(this.input)).toString() | ||
|
||
return jadikanObyek(isiFile) | ||
} | ||
} | ||
|
||
export { EnvDariString, EnvDariFile } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
{ | ||
"name": "envobject", | ||
"version": "1.0.0", | ||
"description": "env to object converter, or object to env converter", | ||
"version": "0.0.1", | ||
"description": "Mengubah env ke object JS", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": ["env", "object", "json"], | ||
"author": "Muhammad Chevalier Joyosentiko", | ||
"author": "Chevalier", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/cj-1010-1414/envobject" | ||
}, | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
PORT=8783 | ||
WANI=yo | ||
NEGARA=Indonesia |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
hsd878hg | ||
mbuh | ||
JI=998 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { EnvDariString, EnvDariFile } from '../induk.js' | ||
|
||
const stringEnv = ` | ||
PORT=8765 | ||
KEY=878yfjhdjhfd8 | ||
SIAP=yes | ||
` | ||
|
||
const stringNonEnv = ` | ||
hsd878hg | ||
mbuh | ||
JI=998 | ||
` | ||
|
||
const hasil = await (new EnvDariFile('./tes/bahan/non-env.env')).keObyek() // (new EnvDariString(stringEnv)).keObyek() | ||
|
||
console.log(hasil) |