0.0.4 • Published 6 years ago

quick-url v0.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

quick-url

A very simple JavaScript URL builder. It is based on ng2-router's navigation API. The query params can be infinitely nested, although the utility of this is limited, it's a nice recursion exercise.

Usage

Simply pass in a resource path in the form of an array and a params object, the params will be parsed to a typical url query string, the params can be recursively nested objects as long as they end up in a string or an array of strings:

  let qUrl = require('quick-url');

  let resource = ['http://example.com', 'users', 'a-123', 'accounts'];
  let params = {
    filter: {
      created: {
        after: '2017-8-6',
        before: '2019-7-5'
      }
    },
    type: ['savings', 'checking'],
  }

  qUrl.buildUrl(resource, params);

  /**
   * http://example.com/users/a-123/accounts?
   *   filter[created][after]=2017-8-6&filter[created][before]=2019-7-5
   *   &type=savings,checking
  */

Conversely, pass in a URL to convert it back into object notation:

  let qUrl = require('quick-url');
  let url = [
    'http://example.com/users/a-123/accounts?',
    'filter[created][after]=2017-8-6&filter[created][before]=2019-7-5',
    '&type=savings,checking'
  ].join('');

  let { resourcePath, queryParams } = qUrl.parseUrl(url);

  /*
    resourcePath = ['http://example.com', 'users', 'a-123', 'accounts'];
    queryParams = {
      filter: {
        created: {
          after: '2017-8-6',
          before: '2019-7-5'
        }
      },
      type: ['savings', 'checking'],
    };
  */

License

MIT

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

7 years ago