New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

objectid

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

objectid - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

.npmignore

36

index.js

@@ -23,5 +23,25 @@ var $3_BITS = 0xFFFFFF;

var makeObjectId = function () {
var ObjectId = function (id) {
if (id && id instanceof ObjectId) {
return id;
}
if (!(this instanceof ObjectId)) {
return new ObjectId(id);
}
/* ObjectIds
if (id) {
id = id.toString()
if (isValid(id)) {
this.id = id;
} else {
throw new Error('Invalid ObjectId: ' + id)
}
} else {
this.id = make();
}
};
var make = function () {
/* ObjectIds
* 24 character hex strings of 12 byte objects

@@ -45,5 +65,13 @@ * 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B

return str;
}
};
ObjectId.prototype._bsontype = 'ObjectID';
ObjectId.prototype.toString = function () {
return this.id;
}
ObjectId.prototype.toJSON = function () {
return this.id;
}
var objIdPattern = /^[0-9a-fA-F]{24}$/;

@@ -57,3 +85,3 @@ var isValid = function (alleged) {

module.exports = makeObjectId;
module.exports = ObjectId;
module.exports.isValid = isValid;

6

package.json
{
"name": "objectid",
"version": "1.1.0",
"version": "2.0.0",
"description": "generate and validate mongodb objectId strings without dependencies",

@@ -24,4 +24,6 @@ "main": "index.js",

"devDependencies": {
"mongodb": "~1.2.12"
"mongodb": "~1.2.12",
"chai": "~1.5.0",
"mocha": "~1.8.2"
}
}

@@ -8,2 +8,4 @@ # objectid

New in 2.x, this module returns objects with the `_bsontype` property set properly to be treated as BSON ObjectIDs, eg, for use in the native driver.
## installation

@@ -18,3 +20,2 @@

var id = objectid()
// =>

@@ -27,4 +28,6 @@ objectid.isValid(id)

New in version 1.1, `objectid.isValid` returns true for `mongodb` native driver `ObjectID` objects, or any other representations with a `.toString` method which returns the hex string encoding of a valid objectid.
`objectid.isValid` returns true for `mongodb` native driver `ObjectID` objects, or any other representations with a `.toString` method which returns the hex string encoding of a valid objectid.
Calling `objectid` with an existing objectid - whether a string, an object created by this module, an objectid created by another driver (such as the result of a query) - will cast the value to an instanceof this module. It will throw if the argument is not a valid ObjectId.
## running the tests

@@ -31,0 +34,0 @@

@@ -1,45 +0,79 @@

var assert = require('assert');
var chai = require('chai')
chai.should()
var expect = chai.expect
var objectId = require('./index')
var ObjectId = require('mongodb').ObjectID
var NativeObjectId = require('mongodb').ObjectID
assert.ok(
objectId.isValid(objectId())
)
describe('objectid', function () {
it ('constructs and object', function () {
objectId().should.be.an('object')
})
assert.ok(
typeof objectId() === 'string'
)
it('has a bsontype to work with the `bson` module', function () {
var id = objectId()
id._bsontype.should.equal('ObjectID');
})
assert.ok(
!objectId.isValid('foo')
)
it('JSON serializes to its id string', function () {
var id = objectId()
JSON.stringify(id).should.equal('"' + id.id + '"')
})
assert.ok(
objectId.isValid('511083bb08ce6b1b00000003')
)
it('casts native driver ObjectIds', function () {
var nativeOid = new NativeObjectId()
var oid = objectId(nativeOid)
oid.should.be.instanceof(objectId)
oid.toString().should.equal(nativeOid.toString())
})
assert.ok(
!objectId.isValid('sdf')
)
it ('casts itself to itself', function () {
var oid = objectId()
var oid2 = objectId(oid)
oid2.should.be.instanceof(objectId)
oid.toString().should.equal(oid2.toString())
})
assert.ok(
!objectId.isValid(123)
)
it('returns the same object if casting an objectId', function () {
var oid = objectId()
var oid2 = objectId(oid)
expect(oid === oid2).to.equal(true)
})
assert.ok(
!objectId.isValid(null)
)
it('casts strings to objectIds', function () {
var oid = '511083bb08ce6b1b00000003'
var oid2 = objectId(oid)
oid.should.equal(oid2.toString())
})
assert.ok(
!objectId.isValid({})
)
it('throws if called as a cast of an invalid objectid', function () {
expect(function () {
var oid = objectId('fsodfisohj')
}).to.throw(/invalid/i)
})
assert.ok(
!objectId.isValid(['foo'])
)
describe('.isValid', function () {
it('validates objectIds as 24 character hex strings', function () {
objectId.isValid('foo').should.equal(false)
objectId.isValid('511083bb08ce6b1b00000003').should.equal(true)
objectId.isValid('sdf').should.equal(false)
objectId.isValid(123).should.equal(false)
objectId.isValid(null).should.equal(false)
objectId.isValid({}).should.equal(false)
objectId.isValid(['foo']).should.equal(false)
})
assert.ok(
objectId.isValid(new ObjectId)
)
it('validates itself', function () {
objectId.isValid(objectId()).should.equal(true)
})
it('validates mongo native driver ObjectIds', function () {
objectId.isValid(new NativeObjectId).should.equal(true)
})
})
})
console.log('all tests pass')
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc