Skip to content
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

feat: copy jsonabc into project #293

Merged
merged 1 commit into from
Jul 24, 2020
Merged
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
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/util/jsonabc.js
src/util/jsonabc.d.ts
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@
"@polkadot/types-known": "^1.23.1",
"@polkadot/util": "^2.17.1",
"@polkadot/util-crypto": "^2.17.1",
"@types/jsonabc": "^2.3.1",
"ajv": "^6.12.2",
"bn.js": "^5.1.2",
"jsonabc": "^2.3.1",
"typescript-logging": "^0.6.4",
"uuid": "^8.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/claim/Claim.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @preferred
*/

import * as jsonabc from 'jsonabc'
import jsonabc from '../util/jsonabc'
import * as SDKErrors from '../errorhandling/SDKErrors'
import IClaim, { CompressedClaim } from '../types/Claim'
import { validateAddress, validateHash } from '../util/DataUtils'
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/Crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { signatureVerify } from '@polkadot/util-crypto'
import blake2AsU8a from '@polkadot/util-crypto/blake2/asU8a'
import naclDecrypt from '@polkadot/util-crypto/nacl/decrypt'
import naclEncrypt from '@polkadot/util-crypto/nacl/encrypt'
import * as jsonabc from 'jsonabc'
import nacl from 'tweetnacl'
import jsonabc from '../util/jsonabc'

export { encodeAddress, decodeAddress, u8aToHex, u8aConcat }

Expand Down
2 changes: 1 addition & 1 deletion src/ctype/CType.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import Ajv from 'ajv'
import * as jsonabc from 'jsonabc'
import jsonabc from '../util/jsonabc'
import Crypto from '../crypto'
import * as SDKErrors from '../errorhandling/SDKErrors'
import IClaim from '../types/Claim'
Expand Down
2 changes: 1 addition & 1 deletion src/requestforattestation/RequestForAttestation.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @preferred
*/

import * as jsonabc from 'jsonabc'
import jsonabc from '../util/jsonabc'
import AttestedClaimUtils from '../attestedclaim/AttestedClaim.utils'
import ClaimUtils from '../claim/Claim.utils'
import * as SDKErrors from '../errorhandling/SDKErrors'
Expand Down
21 changes: 21 additions & 0 deletions src/util/jsonabc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Type definitions for jsonabc 2.3
// Project: http://novicelab.org/jsonabc
// Definitions by: Florian Keller <https://github.com/ffflorian>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
/* eslint-disable */

/**
* Sort the JSON (clean, parse, sort, stringify).
* @param noArray Sort or don't sort arrays
*/
export function sort(inputStr: string, noArray?: boolean): string;

/**
* Sort the JSON (clean, parse, sort, stringify).
* @param noArray Sort or don't sort arrays
*/
export function sortObj<T extends object>(input: T, noArray?: boolean): T;

/** Remove trailing commas */
export function cleanJSON(input: string): string;
93 changes: 93 additions & 0 deletions src/util/jsonabc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Taken from https://github.com/ShivrajRath/jsonabc/blob/2ccf15f967f0e44e48fb7b163aebef43c0047166/index.js
// Copied here, because the package defines a browser compatible script, but doesn't create it,
// which leads to it failing in react-native. See https://github.com/ShivrajRath/jsonabc/issues/18
/* eslint-disable */


/*!
JSON ABC | License: MIT.
*/

module.exports = {
sort: sort,
sortObj: sortObj,
cleanJSON: cleanJSON
};

// Is a value an array?
function isArray (val) {
return Object.prototype.toString.call(val) === '[object Array]';
}

// Is a value an Object?
function isPlainObject (val) {
return Object.prototype.toString.call(val) === '[object Object]';
}

// Sorting Logic
function sortObj (un, noarray) {
noarray = noarray || false;

var or = {};

if (isArray(un)) {
// Sort or don't sort arrays
if (noarray) {
or = un;
} else {
or = un.sort();
}

or.forEach(function (v, i) {
or[i] = sortObj(v, noarray);
});

if (!noarray) {
or = or.sort(function (a, b) {
a = JSON.stringify(a);
b = JSON.stringify(b);
return a < b ? -1 : (a > b ? 1 : 0);
});
}
} else if (isPlainObject(un)) {
or = {};
Object.keys(un).sort(function (a, b) {
if (a.toLowerCase() < b.toLowerCase()) return -1;
if (a.toLowerCase() > b.toLowerCase()) return 1;
return 0;
}).forEach(function (key) {
or[key] = sortObj(un[key], noarray);
});
} else {
or = un;
}

return or;
}

// Remove trailing commas
function cleanJSON (input) {
input = input.replace(/,[ \t\r\n]+}/g, '}');
input = input.replace(/,[ \t\r\n]+\]/g, ']');
return input;
}

// Sort the JSON (clean, parse, sort, stringify).
function sort (inputStr, noarray) {
var output, obj, r;

if (inputStr) {
try {
inputStr = cleanJSON(inputStr);
obj = JSON.parse(inputStr);
r = sortObj(obj, noarray);
output = JSON.stringify(r, null, 4);
} catch (ex) {
console.error('jsonabc: Incorrect JSON object.', [], ex);
throw ex;
}
}
return output;
}

// End.
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -879,11 +879,6 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/jsonabc@^2.3.1":
version "2.3.1"
resolved "https://registry.yarnpkg.com/@types/jsonabc/-/jsonabc-2.3.1.tgz#3aa5d3f938331ef2472d3ea56c6b4c805bef6198"
integrity sha512-SOR8DMQQVUmk3JnNp7FU75U+vp/VcFcghvS68mUQEuq4FKDANIQ9dxDA5qEQMDahBCd5Vyhk81ywn0JbcLpZZw==

"@types/minimist@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
Expand Down Expand Up @@ -3693,11 +3688,6 @@ json5@^1.0.1:
dependencies:
minimist "^1.2.0"

jsonabc@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/jsonabc/-/jsonabc-2.3.1.tgz#800a1bd158fb30ace2b4613f8725adfcb040bfb4"
integrity sha1-gAob0Vj7MKzitGE/hyWt/LBAv7Q=

jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
Expand Down