Giter VIP home page Giter VIP logo

urlset's Introduction

urlset

Application URLs model. Replacing static string URLs with hierarchical model.

Problem

URLs in our apps are hard-coded.

Solution

Building a hierarchical/readable model of URLs. Instead of '\home' we can write url.home(), or '\profile\'+name+'\photos' -> url.profile.photos(name)

Usage

Using urlset is very simple:

	var path	= require('path');
	var urlset	= require('urlset');

	// sitemap.json contains configuration file for urlset
	var links = urlset(require('./sitemap.json'));

	// now, where we need to use link '/' we ca use:
	links.home();

Configuration file

Configuration file(s) are in json format.

Special names

  1. $url - url format for current node;
  2. $path - append path for current node and childs;
  3. $root - ignore parents $path;

Example

{
	"home": "/",
	"item": "/item/${0}-${1}",
	"account": "/account-${name}",
	"accounts": {
		"$path": "/accounts",
		"profile": "/profile",
		"photos": "/${0}/photos",
		"points":"/${0}/points",
		"friends": {
			"$url": "/${0}/friends"
		}
	}
}

Every url in config file can be a string(like "home": "/") or an object: "friends": { "url": "/${0}/friends" }.

This example creates:

	links.home(); // with no params, output: '/'
	links.item(p0,p1); // with 2 params, output: '/item/p0-p1'
	links.accounts.profile(); // with no params, output: '/accounts/profile'
	links.accounts.photos(p0); // with one param, output: '/accounts/p0/photos'
	links.accounts.points(p0); // with one param, output: '/p0/points' !! link is absolute, so it don't adds '/accounts' prefix
	links.accounts.friends(p0); // with one param, output: '/accounts/p0/friends'
	links.account({name: 'uniq', lang: 'ru'}); // with one param, output: '/account-uniq?lang=ru'

All urls accept an extra param(object) for query: links.home({lang:'ru',_ref:'home'}) -> '/?lang=ru&_ref=home'

API

(files, options)

Creates a new links object.

urlset's People

Contributors

mitica avatar dependabot[bot] avatar

Stargazers

Mariano avatar  avatar Art Deineka avatar

Watchers

 avatar

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.