Skip to content

Refactor to Typescript & add TSLint #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea
node_modules
node_modules
index.js
index.d.ts
package-lock.json
4 changes: 0 additions & 4 deletions .npmignore

This file was deleted.

4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
cache: yarn
node_js:
- node
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,42 @@ itself.
## Installation

```
npm i -S eos-rc-parser
npm i -S eos-rc-parser
```



## Usage

```js
const parser = require('eos-rc-parser');
const rcParser = require('eos-rc-parser');

from eosjs signProvider ... signargs.transaction.actions.map(async action => {
const abi = await eos.contract(contractAccountName);
const data = abi.fc.fromBuffer(action.name, action.data);
const actionAbi = abi.fc.abi.actions.find(fcAction => fcAction.name === action.name);


const parsedRicardianContract = parser.parse(action.name, data, actionAbi.ricardian_contract);


const parsedRicardianContract = rcParser.parse(action.name, data, actionAbi.ricardian_contract);

// Optional HTML formatting
const htmlOptions = {h1:'b', h2:'div class="someclass"'};
const parsedRicardianContract = parser.parse(action.name, data, ricardian, signer, true || htmlOptions);
}):
const parsedRicardianContract = rcParser.parse(action.name, data, ricardian, signer, true || htmlOptions);
})
```

Or constitution:

```js
const ricardian = systemAbi.abi.ricardian_clauses[0].body;
const parsedRicardianContract = parser.constitution(ricardian, 'testaccount', {h1:'h1'});
const parsedRicardianContract = rcParser.constitution(ricardian, 'testaccount', {h1:'h1'});
```

## Examples results:

No HTML formatting

```js
parser.parse('bidname', {bid: '3 EOS', bidder: 'testaccount', newname: 'somename'}, ricardian, 'testaccount');
rcParser.parse('bidname', {bid: '3 EOS', bidder: 'testaccount', newname: 'somename'}, ricardian, 'testaccount');
```
```
# Action - "bidname"
Expand All @@ -58,10 +57,9 @@ As an authorized party I "testaccount" wish to bid on behalf of "testaccount" th
HTML formatting.

```js
parser.parse('bidname', {bid: '3 EOS', bidder: 'testaccount', newname: 'somename'}, ricardian, 'testaccount', {h1:'b', h2:'i class="test"'});
rcParser.parse('bidname', {bid: '3 EOS', bidder: 'testaccount', newname: 'somename'}, ricardian, 'testaccount', {h1: 'b', h2: 'i class="test"'});
```
```
<b>Action</b> - "bidname"<br><br><i class="test">Description</i><br><br>The "bidname" action places a bid on a premium account name, in the knowledge that the high bid will purchase the name.<br><br>As an authorized party I "testaccount" wish to bid on behalf of "testac
count" the amount of "3 EOS" toward purchase of the account name "somename".<br>

```
120 changes: 120 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// TYPES /////////////////////////////////////////////////////////

export type HTML = {h1?: string, h2?: string} | null;

// HELPERS //////////////////////////////////////////////////////

/**
* Replaces curly brace placeholders
*
* @private
* @param ricardian
* @param placeholder
* @param replacement
* @returns {*}
*/
const replacePlaceholder = (ricardian: any, placeholder: any, replacement: any) => {
while (ricardian.indexOf(`{{ ${placeholder} }}`) > -1) {
ricardian = ricardian.replace(`{{ ${placeholder} }}`, `"${replacement}"`);
}
return ricardian;
};

/**
* Html can be either a boolean, null, or an object of {h1,h2}
*
* @private
* @param html
* @returns {*}
*/
const htmlDefaults = (html: any) => {
if (html === null) { return html; }
if (html === false) { return null; }

if (typeof html === "boolean") { html = {}; }
if (!html.hasOwnProperty("h1")) { html.h1 = "h1"; }
if (!html.hasOwnProperty("h2")) { html.h2 = "h2"; }

return html;
};

// EXPORTED ///////////////////////////////////////////////////////////

/***
* Parses the constitution
*
* @param {string} constitutionText Constitution Text
* @param {string} signingAccount Signing Account
* @param {Object} [html=null] HTML formatting
* @returns {string} Parsed Constitution
*/
export const constitution = (constitutionText: string, signingAccount: string, html: HTML = null) => {
const getArticleTag = () => constitutionText.match(new RegExp("#" + "(.*)" + "-"));

html = htmlDefaults(html);

// Replacing signer
if (signingAccount) { constitutionText = replacePlaceholder(constitutionText, "signer", signingAccount); }

// Optional HTML formatting.
if (html !== null) {
// Add default fallback to html
if (!html.h1) { html.h1 = "h1"; }
if (!html.h2) { html.h2 = "h2"; }

let articleTag = getArticleTag();

while (articleTag && articleTag[0].length) {
const strippedArticleTag = articleTag[0].replace("# ", "").replace(" -", "");
constitutionText = constitutionText.replace(articleTag[0], `<${html.h1}>${strippedArticleTag}</${html.h1.split(" ")[0]}>`);
articleTag = getArticleTag();
}
constitutionText = constitutionText.replace(/[\n\r]/g, "<br>");
}

return constitutionText;
};

/**
* Parses arbitrary contract action ricardian contracts.
*
* @param {string} actionName Action Name
* @param {Object} actionParameters Action Parameters
* @param {string} ricardianContract Ricardian Contract
* @param {string} signingAccount Signing Account
* @param {Object} [html=null] HTML Formatting
* @returns {string} Parsed Ricardian Contract
*/
export const parse = (actionName: string, actionParameters: any, ricardianContract: string, signingAccount = null, html: HTML = null) => {
html = htmlDefaults(html);

// Stripping backticks
ricardianContract =
ricardianContract.replace(/`/g, "");

// Replacing action name
ricardianContract =
replacePlaceholder(ricardianContract, actionName, actionName);

// Replacing action parameters
Object.keys(actionParameters).map((param) =>
ricardianContract =
replacePlaceholder(ricardianContract, param, actionParameters[param]),
);

// Replacing signer
if (signingAccount) { ricardianContract = replacePlaceholder(ricardianContract, "signer", signingAccount); }

// Optional HTML formatting.
if (html !== null) {
// Add default fallback to html
if (!html.h1) { html.h1 = "h1"; }
if (!html.h2) { html.h2 = "h2"; }

ricardianContract = ricardianContract.replace("# Action", `<${html.h1}>Action</${html.h1.split(" ")[0]}>`);
ricardianContract = ricardianContract.replace("## Description", `<${html.h2}>Description</${html.h2.split(" ")[0]}>`);
ricardianContract = ricardianContract.replace(/[\n\r]/g, "<br>");
}

return ricardianContract;
};
107 changes: 0 additions & 107 deletions lib/index.js

This file was deleted.

Loading