Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Feb 23, 2022
0 parents commit 74914f4
Show file tree
Hide file tree
Showing 9 changed files with 3,183 additions and 0 deletions.
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
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 Boursorama

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.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# TypedJSON

[![npm](https://img.shields.io/npm/v/@boursorama/typed-json.svg)](https://npmjs.com/package/@boursorama/typed-json)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

🚚 A safe and elegant way to deal with Json data in TypeScript inspired by [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON)

## What is it?

Safely interact with Json data by requiring the user to specify what type of data they want to extract.

```typescript
let json = Json.fromString(jsonString)

// Gets the data under `someKey`, if a string returns it otherwise return undefined
json.get('someKey').string()
// Same but provides default empty string if `someKey` does not exists or is not a string
json.get('someKey').stringValue()

// Same exists for all json types
json.get('someKey').boolean()
json.get('someKey').booleanValue() // Will try to coerce the data to bool and returns false if couldn't
json.get('someKey').float()
json.get('someKey').floatValue() // Will try to coerce the data to double and returns 0 if couldn't
json.get('someKey').integer()
json.get('someKey').integerValue() // Will try to coerce the data to int and returns 0 if couldn't

// List items and Map values are wrapped in Json instances, which enables chaining acces to them
json.get('alist').array()
json.get('amap').object()

// If any path element does not exist or is not subscriptable, it'll return undefined
json.get(['you', 'can', 'chain', 'them']).string()

// If you want the unaltered data you can get it with `rawValue`
json.get('some').rawValue

// If you want list and map with unaltered values
json.get('list').arrayRaw()
json.get('map').objectRaw()

// If you get undefined, you can check if its the result of an illegal access
if (json.get('idontexists').exception !== undefined) {
console.log('something went wrong')
}
```
8 changes: 8 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Config } from '@jest/types'

const config: Config.InitialOptions = {
preset: 'ts-jest',
testEnvironment: 'node',
verbose: true
}
export default config
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@boursorama/typed-json",
"version": "1.0.0",
"description": "A safe and elegant way to deal with Json data inspired by SwiftyJSON",
"main": "index.js",
"repository": "[email protected]:boursorama/TypedJSON.git",
"author": "Benoit Giannangeli <[email protected]>",
"license": "MIT",
"private": false,
"devDependencies": {
"@tsconfig/recommended": "^1.0.1",
"@types/jest": "^27.4.0",
"jest": "^27.5.1",
"ts-jest": "^27.1.3",
"ts-node": "^10.5.0",
"typescript": "^4.5.5"
},
"scripts": {
"test": "jest"
}
}
Loading

0 comments on commit 74914f4

Please sign in to comment.