Skip to content

Commit

Permalink
init: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Jun 4, 2019
0 parents commit 5e2095f
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/main.workflow
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
workflow "Build and Publish" {
on = "push"
resolves = ["Publish"]
}

action "Build" {
uses = "actions/npm@master"
args = "install"
}

action "Test" {
needs = ["Build"]
uses = "ianwalter/puppeteer@master"
runs = "npm"
args = "test"
}

action "Publish Filter" {
needs = ["Test"]
uses = "actions/bin/filter@master"
args = "branch master"
}

action "Publish" {
needs = "Publish Filter"
uses = "mikeal/merge-release@master"
secrets = ["GITHUB_TOKEN", "NPM_AUTH_TOKEN"]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
package-lock.json
.nyc_output
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const chalk = require('chalk')
const CID = require('cids')

const _pre = obj => {
if (Array.isArray(obj)) return '['
return '{'
}
const _post = obj => {
if (Array.isArray(obj)) return ']'
return '}'
}

module.exports = (obj, indent = 0, threshold = 80) => {
let pre = Array.from({ length: indent }).map(() => ' ').join('')
let lines = []
for (let key in obj) {
let value = obj[key]
let line
if (Array.isArray(obj)) {
line = ''
} else {
line = chalk.green(key) + ': '
}
if (CID.isCID(value)) lines.push(line + 'CID(' + chalk.red(value.toString()) + ')')
else if (Buffer.isBuffer(value)) lines.push(line + 'Bytes(' + `{${chalk.green('size')}: ${chalk.red(value.length)}})`)
else if (typeof value === 'object' && value !== null) {
lines.push(line + module.exports(value, indent + 2, threshold - line.length))
} else {
lines.push(line + chalk.red(JSON.stringify(value)))
}
}
if (lines.join(', ').length < threshold) return _pre(obj) + ' ' + lines.join(', ') + ' ' + _post(obj)
let ipre = '\n' + pre + ' '
return _pre(obj) + ipre + lines.join(ipre) + '\n' + pre + _post(obj)
}
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@ipld/printify",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Mikeal Rogers <[email protected]> (https://www.mikealrogers.com/)",
"license": "(Apache-2.0 AND MIT)",
"dependencies": {
"chalk": "^2.4.2",
"cids": "^0.7.1"
},
"devDependencies": {
"mocha": "^6.1.4",
"standard": "^12.0.1",
"tsame": "^2.0.1"
}
}
26 changes: 26 additions & 0 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const CID = require('cids')
const assert = require('assert')
const tsame = require('tsame')
const printify = require('../')

const same = (...args) => assert.ok(tsame(...args))
const test = it

test('all', done => {
let str = printify({
one: 1,
two: 'two',
three: {
sub: 1,
cid: new CID('QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u'),
binary: Buffer.from('asdf'),
array: [
'one',
'two',
3
]
}
})
same(str, "{\n \u001b[32mone\u001b[39m: \u001b[31m1\u001b[39m\n \u001b[32mtwo\u001b[39m: \u001b[31m\"two\"\u001b[39m\n \u001b[32mthree\u001b[39m: {\n \u001b[32msub\u001b[39m: \u001b[31m1\u001b[39m\n \u001b[32mcid\u001b[39m: CID(\u001b[31mQmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u\u001b[39m)\n \u001b[32mbinary\u001b[39m: Bytes({\u001b[32msize\u001b[39m: \u001b[31m4\u001b[39m})\n \u001b[32marray\u001b[39m: [ \u001b[31m\"one\"\u001b[39m, \u001b[31m\"two\"\u001b[39m, \u001b[31m3\u001b[39m ]\n }\n}")
done()
})

0 comments on commit 5e2095f

Please sign in to comment.