2.3.0 • Published 4 years ago

fx214 v2.3.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

fx214

MEAN Module Build Status npm version Node.js Version

Hierarchical URL constants in less code.

Installation

npm i fx214

Usage

import buildTree from 'fx214';

const urls = buildTree({
  api: {
    private: {
      newComment: 'new-comment',
      newPost: 'new-post',
    },
  },
});

// `urls` will be like:
/**
{
  api: {
    private: {
      newComment: '/api/private/new-comment',
      newPost: '/api/private/new-post',
    },
  },
}
*/

To customize the value of a URL component, use the special __content__ property:

const urls = buildTree({
  api: {
    p: {
      __content__: 'private',
      newComment: 'new-comment',
      newPost: 'new-post',
    },
  },
});

// `urls` will be like:
/**
{
  api: {
    p: {
      newComment: '/api/private/new-comment',
      newPost: '/api/private/new-post',
    },
  },
}
*/

Falsy values are automatically filled with key names:

const urls = buildTree({
  api: {
    p: {
      add: 0,
      remove: null,
    },
  },
});

// `urls` will be like:
/**
{
  api: {
    p: {
      newComment: '/api/p/add',
      newPost: '/api/p/remove',
    },
  },
}
*/

To replace underscores with hyphens, use Option.underscoresToHyphens:

const urls = buildTree(
  {
    api: {
      user_group: {
        add_user: 'add_user',
        // Falsy values are automatically filled with key names.
        remove_user: 0,
      },
    },
  },
  { underscoresToHyphens: true },
);

// `urls` will be like:
/**
{
  api: {
    p: {
      add_user: '/api/user-group/add-user',
      remove_user: '/api/user-group/remove-user',
    },
  },
}
*/

Use Option.prefix to set a prefix on all resulting URLs:

const urls = buildTree(
  {
    api: {
      user_group: {
        add_user: 'add_user',
        // Falsy values are automatically filled with key names.
        remove_user: 0,
      },
    },
  },
  { prefix: 'https://www.mgenware.com' },
);

/**
{
  api: {
    p: {
      add_user: 'https://www.mgenware.com/api/user-group/add-user',
      remove_user: 'https://www.mgenware.com/api/user-group/remove-user',
    },
  },
}
*/
2.3.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.2.0

4 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago