Skip to content

Commit

Permalink
perubahan total
Browse files Browse the repository at this point in the history
  • Loading branch information
cj-1010-1414 committed Nov 10, 2024
1 parent 3723aa9 commit a1e881f
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 92 deletions.
Empty file removed README.id-ID.md
Empty file.
42 changes: 38 additions & 4 deletions README.md
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
85 changes: 0 additions & 85 deletions index.js

This file was deleted.

63 changes: 63 additions & 0 deletions induk.js
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 }
10 changes: 7 additions & 3 deletions package.json
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"
}
3 changes: 3 additions & 0 deletions tes/bahan/env.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PORT=8783
WANI=yo
NEGARA=Indonesia
3 changes: 3 additions & 0 deletions tes/bahan/non-env.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hsd878hg
mbuh
JI=998
17 changes: 17 additions & 0 deletions tes/induk.tes.js
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)

0 comments on commit a1e881f

Please sign in to comment.