Giter VIP home page Giter VIP logo

relax.js's Introduction

Relax.js

What is it?

Relax.js is a way to have map and reduce couchdb like functions in the browser.

Whay is not?

Relax.js don't connect with couchdb, don't execute views or anything like that (use $.couch for kind of things).

So, when should I use it?

There are times when you have been writing and debuging couchdb views all day. Then, you face a problem in your client code, and think that having a map function just like the one in couchdb will solve the problem. Well, you have Relax.js for that :)

Examples

###The docs:

var docs =[{
		country:{
			name:"Italy",
			cities:[
				{
					name:'Rome'
				},
				{
					name:'Milan'
				},
				{
					name:'Venezia'
				},
			]
		},
	},
	{
		country:{
			name:"Spain",
			cities:[
				{
					name:'Madrid'
				},
				{
					name:'Barcelona'
				},
				{
					name:'Sevilla'
				},
			]
		}
	}
];

So, we have 2 documents, each with a country object, and inside that object a name and an array of cities.

###The map function

	relax.map(docs, function(doc){
		doc.country.cities.forEach(function(city){
			emit([doc.country.name, city.name]);
		});
	});

Here we emit once for every city, where the emitted key is an array containing the country name and the city name

###Result

[
	{"key":["Italy","Milan"] , "value":null},	
	{"key":["Italy","Rome"] , "value":null},
	{"key":["Italy","Venezia"] , "value":null},
	{"key":["Spain","Barcelona"] , "value":null},
	{"key":["Spain","Madrid"] , "value":null},
	{"key":["Spain","Sevilla"] , "value":null}
]

###Emiting values

We a little change in the map function:

	relax.map(docs, function(doc){
		doc.country.cities.forEach(function(city){
			emit(doc.country.name, city.name]);
		});
	});

the result will be:

[
	{"key":"Italy" , "value":"Milan"},
	{"key":"Italy" , "value":"Rome"},
	{"key":"Italy" , "value":"Venezia"},
	{"key":"Spain" , "value":"Barcelona"},
	{"key":"Spain" , "value":"Madrid"},
	{"key":"Spain" , "value":"Sevilla"}
]

Features:

  • couchdb like map
  • couchdb like reduce (no docs yet, but you can explore the tests

ToDo:

  • Make the results for both function get sorted just like couchdb would sort them
  • There are more ToDo tasks in the issues

relax.js's People

Stargazers

 avatar  avatar Wojciech Bednarski avatar Fredrik Forsmo avatar

Watchers

James Cloos avatar

relax.js's Issues

Write examples for reduce function

reduce function only works with objects with this format: {key:xx, value:xx}, and that should be written. Also the existence of the function sum should we described

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.