Giter VIP home page Giter VIP logo

node-ni-uri's Introduction

NI-URI

Parsing, formatting and digesting utilities for Named Information (NI) URIs in Node.js land.

Usage

install / require

$ npm install ni-uri

const ni = require('ni-uri');

.format(Object parts)

Similar to Node.js' url#format().

const result = ni.format({
  host: example.com,
  algorithm: 'sha-256',
  value: '71N_JciVv6eCUmUpqbY9l6pjFWTV14nCt2VEjIY1-2w',
  query: {q: '1'}
});

// 'ni://example.con/sha-256;71N_JciVv6eCUmUpqbY9l6pjFWTV14nCt2VEjIY1-2w?q=1'

.parse(string uri, boolean parseQuery)

Similar to Node.js' url#parse().

const result = ni.parse('ni://example.con/sha-256;71N_JciVv6eCUmUpqbY9l6pjFWTV14nCt2VEjIY1-2w?q=1', true);
 
// {
//   protocol: 'ni',
//   algorithm: 'sha-256',
//   value: '71N_JciVv6eCUmUpqbY9l6pjFWTV14nCt2VEjIY1-2w',
//   host: 'example.com'
// }

.digest([string algorithm], string | Buffer data, [string encoding], [Object | boolean parts])

Generates the hash/value component for some data. Returns the hash or a formatted uri.

const result = ni.digest('sha-256', 'The quick brown fox jumps over the lazy dog.');

// returns '71N_JciVv6eCUmUpqbY9l6pjFWTV14nCt2VEjIY1-2w'

const result = ni.digest('sha-256', 'The quick brown fox jumps over the lazy dog.', true);

// returns 'ni:///sha-256;71N_JciVv6eCUmUpqbY9l6pjFWTV14nCt2VEjIY1-2w'

const result = ni.digest('sha-256', 'The quick brown fox jumps over the lazy dog.', {host: 'example.com'});

// returns 'ni://example.com/sha-256;71N_JciVv6eCUmUpqbY9l6pjFWTV14nCt2VEjIY1-2w'

.isAlgorithm(string algorithm)

Returns true if the string identifies a supported digest algorithm, false otherwise.

To-Do

  • support for human-friendly nih:// format
  • support for hash truncation

License

MIT - see LICENSE.md.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.