Skip to content

Commit

Permalink
chore: ts,readme
Browse files Browse the repository at this point in the history
Add Typescript Proclamations and Readme

fix #2
  • Loading branch information
Changlon committed Jan 29, 2022
1 parent cd28a37 commit 5447f7a
Show file tree
Hide file tree
Showing 8 changed files with 693 additions and 27 deletions.
45 changes: 45 additions & 0 deletions .github/README_tpl.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# f2json

[![NPM version](https://img.shields.io/npm/v/f2json)](https://npmjs.org/package/f2json) [![Build Status](https://img.shields.io/circleci/build/github/Changlon/f2json/master)](https://app.circleci.com/pipelines/github/Changlon/f2json) [![Codecov](https://img.shields.io/codecov/c/github/Changlon/f2json)](https://app.codecov.io/github/Changlon/f2json) [![GitHub issues](https://img.shields.io/github/issues/Changlon/f2json)](https://github.com/Changlon/f2json/issues) [![GitHub license](https://img.shields.io/github/license/Changlon/f2json)](https://github.com/Changlon/f2json/blob/master/LICENSE)

> Quickly create read-write json files [f2json](https://github.com/Changlon/f2json)

* Quick Import and write out the JSON data to the file
* Introduce a Json file operation in your code using a relative path
* You can use the created file as a temporary cache


{{#module name="f2json"}}{{>body}}{{/module}}


## Installation

### npm
```sh
npm install f2json
```

### yarn

```sh
yarn add f2json
```

## API Reference
{{#module name="f2json"~}}
{{>body~}}
{{>member-index~}}
{{>members~}}
{{/module~}}

## Contributing

Please submit all issues and pull requests to the [Changlon/f2json](https://github.com/Changlon/f2json/issues) repository!

## Tests

Run tests using `npm test`.

## Support

If you have any problem or suggestion please open an issue [here](https://github.com/Changlon/f2json/issues).
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "pwa-node",
"request": "launch",
Expand Down
114 changes: 106 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,106 @@
# f2json
Quickly create read-write json files

![CircleCI](https://img.shields.io/circleci/build/github/Changlon/f2json/master)
![npm](https://img.shields.io/npm/v/f2json)
![Codecov](https://img.shields.io/codecov/c/github/Changlon/f2json)
[![GitHub issues](https://img.shields.io/github/issues/Changlon/f2json)](https://github.com/Changlon/f2json/issues)
[![GitHub license](https://img.shields.io/github/license/Changlon/f2json)](https://github.com/Changlon/f2json/blob/master/LICENSE)
# f2json

[![NPM version](https://img.shields.io/npm/v/f2json)](https://npmjs.org/package/f2json) [![Build Status](https://img.shields.io/circleci/build/github/Changlon/f2json/master)](https://app.circleci.com/pipelines/github/Changlon/f2json) [![Codecov](https://img.shields.io/codecov/c/github/Changlon/f2json)](https://app.codecov.io/github/Changlon/f2json) [![GitHub issues](https://img.shields.io/github/issues/Changlon/f2json)](https://github.com/Changlon/f2json/issues) [![GitHub license](https://img.shields.io/github/license/Changlon/f2json)](https://github.com/Changlon/f2json/blob/master/LICENSE)

> Quickly create read-write json files [f2json](https://github.com/Changlon/f2json)
* Quick Import and write out the JSON data to the file
* Introduce a Json file operation in your code using a relative path
* You can use the created file as a temporary cache





## Installation

### npm
```sh
npm install f2json
```

### yarn

```sh
yarn add f2json
```

## API Reference

* [f2json](#module_f2json)
* [F2Json](#exp_module_f2json--F2Json)
* [new F2Json([dirname])](#new_module_f2json--F2Json_new)
* [.getLastCallDirname()](#module_f2json--F2Json+getLastCallDirname) ⇒ <code>String</code>
* [.json2file(path, json)](#module_f2json--F2Json+json2file)
* [.file2json(path, encoding)](#module_f2json--F2Json+file2json) ⇒ <code>Object.&lt;json, ok&gt;</code>
* [.clear(path_)](#module_f2json--F2Json+clear)

<a name="exp_module_f2json--F2Json"></a>

### F2Json ⏏
**Kind**: Exported class
<a name="new_module_f2json--F2Json_new"></a>

#### new F2Json([dirname])
Create f2Json


| Param | Type | Description |
| --- | --- | --- |
| [dirname] | <code>String</code> | Optional,You can also pass in a relative path such as ```__dirname``` in your code file |

**Example**
Basic usage: ```javascriptindex.js :var F2Json = require("f2json") var f2json = new F2Json()f2json.json2file("./cache/user.json",[ {name:"Changlon",age:22}, ...])var {json} = f2json.file2json("./cache/user.json") console.log(json) ```
<a name="module_f2json--F2Json+getLastCallDirname"></a>

#### f2Json.getLastCallDirname() ⇒ <code>String</code>
Returns the file path that called this function

**Kind**: instance method of [<code>F2Json</code>](#exp_module_f2json--F2Json)
<a name="module_f2json--F2Json+json2file"></a>

#### f2Json.json2file(path, json)
Writes the json object as data to the specified fileWrites the json object as data to the specified file

**Kind**: instance method of [<code>F2Json</code>](#exp_module_f2json--F2Json)

| Param | Type |
| --- | --- |
| path | <code>String</code> |
| json | <code>Object</code> |

<a name="module_f2json--F2Json+file2json"></a>

#### f2Json.file2json(path, encoding) ⇒ <code>Object.&lt;json, ok&gt;</code>
Read the file as a Json object in Json format

**Kind**: instance method of [<code>F2Json</code>](#exp_module_f2json--F2Json)
**Returns**: <code>Object.&lt;json, ok&gt;</code> - returns data json and function ok When you call the OK function, the modified data json is saved to the corresponding file

| Param | Type |
| --- | --- |
| path | <code>String</code> |
| encoding | <code>String</code> |

<a name="module_f2json--F2Json+clear"></a>

#### f2Json.clear(path_)
Type an empty json object into the specified file

**Kind**: instance method of [<code>F2Json</code>](#exp_module_f2json--F2Json)

| Param | Type |
| --- | --- |
| path_ | <code>String</code> |

## Contributing

Please submit all issues and pull requests to the [Changlon/f2json](https://github.com/Changlon/f2json/issues) repository!

## Tests

Run tests using `npm test`.

## Support

If you have any problem or suggestion please open an issue [here](https://github.com/Changlon/f2json/issues).
Expand Down
26 changes: 26 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @author Changlon <[email protected]>
* @github https://github.com/Changlon
* @date 2022-01-29 22:55:57
*/

declare class F2Json {
dirname:string
constructor(dirname?:string)
public getLastCallDirname():string
public json2file(path_:string,json:{[k:string]:any}):void
public file2json (path_:string,encoding:string):{
json:{[k:string]:any},
ok:()=>void
}
public clear(path_:string):void
}



declare namespace F2Json {
interface F2JsonError extends Error {}
type a = {a:string}
}

export = F2Json
45 changes: 33 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@
/**
* Quickly create read-write json files
*
* @author Changlon <thinker_changlon@163.com>
* @author Changlon <changlong.a2@gmail.com>
* @github https://github.com/Changlon
*/



var fs = require('fs')
var path = require('path')
var callsite = require('callsite')
const { sep } = require('path')

class F2JsonError extends Error{
constructor(message) {
super(message)
}
}


var { sep } = require('path')



Expand All @@ -30,11 +21,36 @@ class F2JsonError extends Error{

module.exports = F2Json
module.exports.F2JsonError = F2JsonError

/**
* Create f2Json
*
* @param {String} dirname
* @example
*
* Basic usage:
*
* ```javascript
*
* index.js :
*
* var F2Json = require("f2json")
* var f2json = new F2Json()
*
* f2json.json2file("./cache/user.json",[
* {name:"Changlon",age:22},
* ...
* ])
*
* var {json} = f2json.file2json("./cache/user.json")
*
* console.log(json)
*
* ```
* @alias module:f2json
* @param {String=} dirname Optional,You can also pass in a relative path such as ```__dirname``` in your code file
* @constructor
*/

function F2Json(dirname) {
if(!(this instanceof F2Json)) return new F2Json(dirname)
this.dirname = dirname || this.getLastCallDirname()
Expand Down Expand Up @@ -167,3 +183,8 @@ F2Json.prototype.clear = function(path_) {
}
}

class F2JsonError extends Error{
constructor(message) {
super(message)
}
}
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
"scripts": {
"test": "istanbul cover -x *.test.js node_modules/mocha/bin/_mocha -- -R spec test/*.test.js",
"check-coverage": "istanbul check-coverage --statements 85 --branches 85 --functions 100 --lines 90",
"report-coverage":"cat ./coverage/lcov.info | codecov",
"report-coverage": "cat ./coverage/lcov.info | codecov",
"test:w": "mocha -w",
"semantic-release": "semantic-release",
"commit": "cz"
"commit": "cz",
"docs":"jsdoc2md index.js --template .github/README_tpl.hbs > README.md"
},
"devDependencies": {
"chai": "4.3.6",
Expand All @@ -36,12 +37,17 @@
"cz-conventional-changelog": "3.3.0",
"ghooks": "2.0.4",
"istanbul": "0.4.5",
"jsdoc-to-markdown": "7.1.1",
"mocha": "9.2.0",
"semantic-release": "^19.0.2"
},
"dependencies": {
"callsite": "1.0.0"
},
"files": [
"index.d.ts",
"index.js"
],
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require("fs")
const path = require("path")
const { expect } = require("chai")
const F2Json = require("..")
const F2Json = require('..')
const {F2JsonError} = F2Json

describe("f2json",()=>{
Expand Down
Loading

0 comments on commit 5447f7a

Please sign in to comment.