feat(browser): add browser build, tests, and sample sha256 library method
This commit is contained in:
8
src/adapters/crypto.browser.ts
Normal file
8
src/adapters/crypto.browser.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
// Must first be built by browserify.
|
||||
// https://github.com/rollup/rollup-plugin-commonjs/issues/105#issuecomment-281917166
|
||||
import hash from 'hash.js'
|
||||
|
||||
export function createHash (algorithm: 'sha256') {
|
||||
console.log(hash)
|
||||
return hash.sha256()
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import { test } from 'ava'
|
||||
import * as lib from './'
|
||||
|
||||
test('functions can be used without es imports', (t) => {
|
||||
t.true(typeof lib.double === 'function')
|
||||
t.true(typeof lib.power === 'function')
|
||||
t.true(typeof lib.asyncABC === 'function')
|
||||
})
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from './lib/numberOps'
|
||||
export * from './lib/asyncOps'
|
||||
export * from './lib/async'
|
||||
export * from './lib/hash'
|
||||
export * from './lib/number'
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { test } from 'ava'
|
||||
import { asyncABC } from '../'
|
||||
import { asyncABC } from 'typescript-starter'
|
||||
|
||||
test('getABC', async t => {
|
||||
t.deepEqual(await asyncABC(), ['a','b', 'c'])
|
||||
6
src/lib/hash.spec.ts
Normal file
6
src/lib/hash.spec.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { test } from 'ava'
|
||||
import { sha256 } from 'typescript-starter'
|
||||
|
||||
test('sha256', t => {
|
||||
t.deepEqual(sha256('test'), '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')
|
||||
})
|
||||
17
src/lib/hash.ts
Normal file
17
src/lib/hash.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { createHash } from 'crypto'
|
||||
|
||||
/**
|
||||
* Calculate the sha256 digest of a string. On Node.js, this will use the native module, in the browser, it will fall back to a pure javascript implementation.
|
||||
*
|
||||
* ### Example (es imports)
|
||||
* ```js
|
||||
* import { sha256 } from 'typescript-starter'
|
||||
* sha256('test')
|
||||
* // => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08'
|
||||
* ```
|
||||
*
|
||||
* @returns sha256 message digest
|
||||
*/
|
||||
export function sha256 (message: string) {
|
||||
return createHash('sha256').update(message).digest('hex')
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { test } from 'ava'
|
||||
import { double, power } from '../'
|
||||
import { double, power } from 'typescript-starter'
|
||||
|
||||
test('double', t => {
|
||||
t.deepEqual(double(2), 4)
|
||||
Reference in New Issue
Block a user