Git Product home page Git Product logo

caseless's Introduction

Caseless -- wrap an object to set and get property with caseless semantics but also preserve caseing.

This library is incredibly useful when working with HTTP headers. It allows you to get/set/check/delete headers in a caseless manner while also preserving the headers' case when they are first set.

Usage

var headers = {}
  , c = caseless(headers)
  ;
c.set('a-Header', 'asdf')
c.get('a-header') === 'asdf'

has(key)

Has takes a name and if it finds a matching header will return that header name with the preserved caseing it was set with.

c.has('a-header') === 'a-Header'

set(key, value[, clobber=true])

Set is fairly straight forward except that if the header exists and clobber is disabled it will add ','+value to the existing header.

c.set('a-Header', 'fdas')
c.set('a-HEADER', 'more', false)
c.get('a-header') === 'fdsa,more'

swap(key)

Swaps the casing of a header with the new one that is passed in.

var headers = {}
  , c = caseless(headers)
  ;
c.set('a-Header', 'fdas')
c.swap('a-HEADER')
c.has('a-header') === 'a-HEADER'
headers === {'a-HEADER': 'fdas'}

del(key)

Deletes a key and, if there's many instances of the key with multiple cases, all of them.

var headers = {
  'a-Header': true,
  'content-length': 312,
  'Content-Length': 312
}
var c = caseless(headers);

c.del('Content-length');
headers === {
  'a-Header': true
};

caseless's People

Contributors

eiriksm avatar ganeshkumar1989 avatar ktk avatar mark-bradshaw avatar mikeal avatar nylen avatar pajtai avatar plauclair avatar ttahmouch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

caseless's Issues

unable to install caseless

Hi @mikeal I'm unable to install caseless with

npm install caseless

NPM error:

npm ERR! Error: EACCES, mkdir '/Users/n/.npm/caseless/0.8.0'
npm ERR!  { [Error: EACCES, mkdir '/Users/n/.npm/caseless/0.8.0']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/Users/n/.npm/caseless/0.8.0',
npm ERR!   parent: 'request' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 13.3.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/n/play/hapi_template
npm ERR! node -v v0.10.33
npm ERR! npm -v 1.4.28
npm ERR! path /Users/n/.npm/caseless/0.8.0
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCE

Do I need to install _globally_ or with sudo?

License for version 0.9.0 and lower

Hello @mikeal

Currently I'm using this component under version 0.9.0 and 0.6.0 which seems not to be present on your repository. The only information regarding the license for the version 0.9.0 and lower ones is the statement present inside the package.json file, which is "BSD".
Would you please be more specific regarding the license for the version 0.9.0 and older ones? What license will apply for those versions, Apache 2.0 (as I can see is the master license) or the BSD one? And in case of BSD which version?

Thank you!

Clarify license

Could you please clarify the license this software is distributed under?

package.json says BSD, but there is no license file in the repo. Since BSD family licenses typically require distributing an exact copy of the license alongside the software, and there isn't one canonical version of the BSD license, I can't get the legal department OK to use this software.

I know it's pedantic, but you know how lawyers are =(

Thanks!

swap function removes the header if already exists

Hello!
When using swap function, if casing of passed argument matches the current header, it simply gets removed. It is very easy to reproduce

  options.headers = {'Content-Type':'something'}
  normalizedHeaders = caseless options.headers
  normalizedHeaders.swap 'Content-Type'

Content-Type is now vanished.

Ref line

get incorrectly matches other headers containing name

get incorrectly matches other headers containing name. See this example:

var assert = require('assert'),
     caseless = require('caseless');

var c = caseless({
     'Foo': 'bar',
     'Not-Foo': 'baz'
});

assert.equal(c.get('Foo'), 'bar');

which gives output:

assert.js:92
throw new assert.AssertionError({
^
AssertionError: "baz" == "bar"
at Object. (bug.js:9:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3

Very similar to: request/request#825 ;-)

API change suggestion on set(key, value[, clobber=true])

Just a small thing, and you don't have to agree with me but, I consider that in these cases it would be more readable an API with a third param as an options object like:

set(key, value[, {clobber: true}]);

since when in code you know what that false/true means:

c.set('a-HEADER', 'more', {clobber: false});

What do you think?

Leaking global variable: _key

This is tripping global leak detection in mocha in any project using this module.

Caseless.prototype.get = function (name) {
  name = name.toLowerCase()
  var result, key
  var headers = this.dict
  Object.keys(headers).forEach(function (key) {
    _key = key.toLowerCase()
    if (name === _key) result = headers[key]
  })
  return result
}

has is not equivalent to official HTTP APIs

http and https module implement hasHeader which returns a boolean.

But caseless returns either false or the header name which is not the same.

https://www.npmjs.com/package/caseless#haskey

Caseless.prototype.has = function (name) {
  var keys = Object.keys(this.dict)
    , name = name.toLowerCase()
    ;
  for (var i=0;i<keys.length;i++) {
    if (keys[i].toLowerCase() === name) return keys[i]
  }
  return false
}

Same for the official HTTP APIs in browsers, see https://developer.mozilla.org/en-US/docs/Web/API/Headers/has

License document

Hey,
Thanks for the good effort in building this library. We would like to use your node in our project.

Can you let us know what BSD License have been assigned to this library?

Thanks
Kandy

Publish latest version

Please publish the latest version of caseless which switched to Apache 2 license and includes a license file! Thank you!

del() doesn't work correctly on key duplicates

I sent a PR #29 to fix an issue where del() won't delete duplicates if they exist. I'm assuming since set() and get() avoid duplicates, delete should be doing the same. You can see in more details in the tests the behavior.

I also updated the readme.

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.