Question

I'm working on a web service that needs to accept categories and perform a search using those. Categories can be combined, hence bitmask comes to mind.

Example:

Spring = 1, Summer = 2, Autumn = 4, Winter = 8

Possible options:

  1. ?categories=5 - not very user friendly/pretty
  2. ?categories=1,4 - needs special parsing
  3. ?categories=1&categories=4 - well supported but a bit verbose for a lot of categories
  4. ?categories=Spring,Autumn - seems most user friendly

Is there any standard way or preferred way to model bitmask type data?

Was it helpful?

Solution

I'd suggest going for semantic clarity over compression, but leverage native functions like JSON.stringify() and JSON.parse() and model them as an array, e.g.

categories = ['spring', 'summer', 'winter']

This is like #4, but slightly different in that it's using JSON which you can generate and parse unambiguously.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top