Skip to content

Commit

Permalink
Escrita e leitura de arquivos JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonelsanchesdasilva committed Dec 16, 2022
0 parents commit 9317a50
Show file tree
Hide file tree
Showing 11 changed files with 2,567 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
coverage/
dist/
node_modules/

testes/exemplo2.json
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Testes unitários",
"type": "node",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/jest/bin/jest.js",
"--runInBand"
],
"skipFiles": ["<node_internals>/**", "node_modules/**"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Design Líquido

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# delegua-json

Biblioteca para manipulação de JSON.
13 changes: 13 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as sistemaArquivos from "node:fs"
import * as caminho from "node:path"

export function importarArquivoJson(caminhoArquivo: string): any {
const caminhoResolvido = caminho.resolve(caminhoArquivo);
const dadosDoArquivo: Buffer = sistemaArquivos.readFileSync(caminhoResolvido);
return JSON.parse(dadosDoArquivo.toString());
}

export function exportarObjetoParaArquivoJson(conteudoJson: any, caminhoArquivo: string) {
const conteudoTratado = JSON.stringify(conteudoJson);
sistemaArquivos.writeFileSync(caminhoArquivo, conteudoTratado);
}
11 changes: 11 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Config } from '@jest/types';

export default async (): Promise<Config.InitialOptions> => {
return {
verbose: true,
modulePathIgnorePatterns: ['<rootDir>/dist/'],
preset: 'ts-jest',
testEnvironment: 'node',
coverageReporters: ['text', 'text-summary', 'lcov'],
};
};
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@designliquido/delegua-json",
"description": "Biblioteca para Delégua e JavaScript para manipulação de JSON.",
"author": "Leonel Sanches da Silva",
"scripts": {
"testes-unitarios": "jest --coverage"
},
"devDependencies": {
"@types/jest": "^29.2.4",
"@types/node": "^18.11.15",
"jest": "^29.3.1",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
}
}
117 changes: 117 additions & 0 deletions testes/arquivos.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { exportarObjetoParaArquivoJson, importarArquivoJson } from "..";

describe("Métodos que trabalham com arquivos", () => {
it("Arquivo para JSON", () => {
const retorno = importarArquivoJson("./testes/exemplo.json");
expect(retorno).toBeTruthy();
expect(retorno.funcionarios).toBeTruthy();
expect(retorno.funcionarios).toHaveLength(11);
expect(retorno.areas).toBeTruthy();
expect(retorno.areas).toHaveLength(3);
});

it("JSON para arquivo", () => {
const retorno = exportarObjetoParaArquivoJson({
"funcionarios": [
{
"id": 0,
"nome": "Marcelo",
"sobrenome": "Silva",
"salario": 3200.00,
"area": "SM"
},
{
"id": 1,
"nome": "Washington",
"sobrenome": "Ramos",
"salario": 2700.00,
"area": "UD"
},
{
"id": 2,
"nome": "Sergio",
"sobrenome": "Pinheiro",
"salario": 2450.00,
"area": "SD"
},
{
"id": 3,
"nome": "Bernardo",
"sobrenome": "Costa",
"salario": 3700.00,
"area": "SM"
},
{
"id": 4,
"nome": "Cleverton",
"sobrenome": "Farias",
"salario": 2750.00,
"area": "SD"
},
{
"id": 5,
"nome": "Abraão",
"sobrenome": "Campos",
"salario": 2550.00,
"area": "SD"
},
{
"id": 6,
"nome": "Letícia",
"sobrenome": "Farias",
"salario": 2450.00,
"area": "UD"
},
{
"id": 7,
"nome": "Fernando",
"sobrenome": "Ramos",
"salario": 2450.00,
"area": "SD"
},
{
"id": 8,
"nome": "Marcelo",
"sobrenome": "Farias",
"salario": 2550.00,
"area": "UD"
},
{
"id": 9,
"nome": "Fabio",
"sobrenome": "Souza",
"salario": 2750.00,
"area": "SD"
},
{
"id": 10,
"nome": "Clederson",
"sobrenome": "Oliveira",
"salario": 2500.00,
"area": "SD"
}
],
"areas": [
{
"codigo": "SD",
"nome": "Desenvolvimento de Software"
},
{
"codigo": "SM",
"nome": "Gerenciamento de Software"
},
{
"codigo": "UD",
"nome": "Designer de UI/UX"
}
]
}, "./testes/exemplo2.json");

const jsonExportado = importarArquivoJson("./testes/exemplo2.json");
expect(jsonExportado).toBeTruthy();
expect(jsonExportado.funcionarios).toBeTruthy();
expect(jsonExportado.funcionarios).toHaveLength(11);
expect(jsonExportado.areas).toBeTruthy();
expect(jsonExportado.areas).toHaveLength(3);
});
});
95 changes: 95 additions & 0 deletions testes/exemplo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"funcionarios": [
{
"id": 0,
"nome": "Marcelo",
"sobrenome": "Silva",
"salario": 3200.00,
"area": "SM"
},
{
"id": 1,
"nome": "Washington",
"sobrenome": "Ramos",
"salario": 2700.00,
"area": "UD"
},
{
"id": 2,
"nome": "Sergio",
"sobrenome": "Pinheiro",
"salario": 2450.00,
"area": "SD"
},
{
"id": 3,
"nome": "Bernardo",
"sobrenome": "Costa",
"salario": 3700.00,
"area": "SM"
},
{
"id": 4,
"nome": "Cleverton",
"sobrenome": "Farias",
"salario": 2750.00,
"area": "SD"
},
{
"id": 5,
"nome": "Abraão",
"sobrenome": "Campos",
"salario": 2550.00,
"area": "SD"
},
{
"id": 6,
"nome": "Letícia",
"sobrenome": "Farias",
"salario": 2450.00,
"area": "UD"
},
{
"id": 7,
"nome": "Fernando",
"sobrenome": "Ramos",
"salario": 2450.00,
"area": "SD"
},
{
"id": 8,
"nome": "Marcelo",
"sobrenome": "Farias",
"salario": 2550.00,
"area": "UD"
},
{
"id": 9,
"nome": "Fabio",
"sobrenome": "Souza",
"salario": 2750.00,
"area": "SD"
},
{
"id": 10,
"nome": "Clederson",
"sobrenome": "Oliveira",
"salario": 2500.00,
"area": "SD"
}
],
"areas": [
{
"codigo": "SD",
"nome": "Desenvolvimento de Software"
},
{
"codigo": "SM",
"nome": "Gerenciamento de Software"
},
{
"codigo": "UD",
"nome": "Designer de UI/UX"
}
]
}
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2017",
"rootDir": ".",
"allowJs": true,
"sourceMap": true,
"declaration": true,
"esModuleInterop": true
},
"exclude": ["babel.config.js", "dist/**/*"]
}
Loading

0 comments on commit 9317a50

Please sign in to comment.