-
Notifications
You must be signed in to change notification settings - Fork 8
feat(mongodb-ns)!: move ns package #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e336ceb
move-ns: try to move ns package
johnjackweir 9269e6f
use apache license like the other packages
johnjackweir 9e9b469
create using workspace
johnjackweir 3a16aec
missing files
johnjackweir a696251
typescript
johnjackweir 682e3f3
typescript
johnjackweir 3ce03aa
typescript
johnjackweir 3865130
typescript
johnjackweir 8cabb4a
typescript
johnjackweir 2a5c2a9
fix version
johnjackweir 2f1f203
unused deps
johnjackweir 311811f
Update packages/mongodb-ns/src/index.ts
johnjackweir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": [ | ||
"mongodb-js/node" | ||
] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
node_modules/ | ||
.dist/ | ||
.build/ | ||
npm-debug.log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.travis.yml | ||
test.js | ||
.eslintrc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Lucas Hrabovsky <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# mongodb-ns | ||
|
||
Handle dem namespaces like the kernel do. | ||
|
||
``` | ||
var ns = require('mongodb-ns'); | ||
|
||
var bacon = ns('canadian-things.songs-aboot-bacon'); | ||
console.log(bacon.toString() + '\n', bacon); | ||
``` | ||
|
||
will output | ||
|
||
``` | ||
canadian-things.songs-aboot-bacon | ||
NS { | ||
ns: 'canadian-things.songs-aboot-bacon', | ||
dotIndex: 15, | ||
database: 'canadian-things', | ||
collection: 'songs-aboot-bacon', | ||
system: false, | ||
oplog: false, | ||
command: false, | ||
special: false, | ||
specialish: false, | ||
normal: true, | ||
validDatabaseName: true, | ||
validCollectionName: true, | ||
databaseHash: 23620443216 } | ||
``` | ||
|
||
|
||
## license | ||
|
||
MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class NS { | ||
ns: string; | ||
dotIndex: number; | ||
database: string; | ||
collection: string; | ||
system: boolean; | ||
isSystem(): boolean; | ||
oplog: boolean; | ||
isOplog(): boolean; | ||
command: boolean; | ||
isCommand(): boolean; | ||
special: boolean; | ||
isSpecial(): boolean; | ||
specialish: boolean; | ||
normal: boolean; | ||
isNormal(): boolean; | ||
validDatabaseName: boolean; | ||
validCollectionName: boolean; | ||
databaseHash: number; | ||
toString(): string; | ||
static sort(namespaces: (string | NS)[]): typeof namespaces; | ||
static MAX_DATABASE_NAME_LENGTH: number; | ||
// Assigned in the constructor, but will always be undefined | ||
// isConf: undefined; | ||
} | ||
|
||
declare const toNS: ((namespace: string | NS) => NS) & { | ||
sort: typeof NS['sort']; | ||
}; | ||
|
||
export default toNS; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
var types = ['Command', 'Special', 'System', 'Oplog', 'Normal', 'Conf']; | ||
|
||
// eslint-disable-next-line complexity | ||
function NS(ns) { | ||
if (!(this instanceof NS)) { | ||
return new NS(ns); | ||
} | ||
|
||
this.ns = ns; | ||
this.dotIndex = ns.indexOf('.'); | ||
if (this.dotIndex === -1) { | ||
this.database = ns; | ||
this.collection = ''; | ||
} else { | ||
this.database = ns.slice(0, this.dotIndex); | ||
this.collection = ns.slice(this.dotIndex + 1); | ||
} | ||
|
||
this.system = /^(?:system(?!\.profile$).*|enxcol_)\./.test(this.collection); | ||
|
||
this.oplog = /local\.oplog\.(\$main|rs)/.test(ns); | ||
|
||
this.command = | ||
this.collection === '$cmd' || this.collection.indexOf('$cmd.sys') === 0; | ||
this.special = | ||
this.oplog || this.command || this.system || this.database === 'config' || /^__mdb_internal_\w/.test(this.database); | ||
|
||
this.specialish = | ||
this.special || ['local', 'admin'].indexOf(this.database) > -1; | ||
|
||
this.normal = this.oplog || this.ns.indexOf('$') === -1; | ||
|
||
/** | ||
* @note (imlucas) The following are not valid on windows: | ||
* `*<>:|?` | ||
*/ | ||
this.validDatabaseName = | ||
new RegExp('^[^\\\\/". ]*$').test(this.database) && | ||
this.database.length <= NS.MAX_DATABASE_NAME_LENGTH; | ||
this.validCollectionName = | ||
this.collection.length > 0 && | ||
(this.oplog || /^[^\0\$]*$/.test(this.collection)); | ||
|
||
this.databaseHash = 7; | ||
this.ns.split('').every( | ||
function(c, i) { | ||
if (c === '.') { | ||
return false; | ||
} | ||
this.databaseHash += 11 * this.ns.charCodeAt(i); | ||
this.databaseHash *= 3; | ||
return true; | ||
}.bind(this) | ||
); | ||
} | ||
|
||
NS.prototype.database = ''; | ||
NS.prototype.databaseHash = 0; | ||
NS.prototype.collection = ''; | ||
|
||
NS.prototype.command = false; | ||
NS.prototype.special = false; | ||
NS.prototype.system = false; | ||
NS.prototype.oplog = false; | ||
NS.prototype.normal = false; | ||
NS.prototype.specialish = false; | ||
|
||
types.forEach(function(type) { | ||
NS.prototype['is' + type] = function() { | ||
return this[type.toLowerCase()]; | ||
}; | ||
}); | ||
|
||
NS.prototype.toString = function() { | ||
return this.ns; | ||
}; | ||
|
||
NS.MAX_DATABASE_NAME_LENGTH = 128; | ||
|
||
module.exports = NS; | ||
|
||
var ns = NS; | ||
module.exports.sort = function(namespaces) { | ||
namespaces.sort(function(a, b) { | ||
if (ns(a).specialish && ns(b).specialish) { | ||
return 0; | ||
} | ||
if (ns(a).specialish && !ns(b).specialish) { | ||
return 1; | ||
} | ||
if (!ns(a).specialish && ns(b).specialish) { | ||
return -1; | ||
} | ||
return a > b ? 1 : -1; | ||
}); | ||
return namespaces; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "mongodb-ns", | ||
"version": "2.4.3", | ||
"author": { | ||
"name": "MongoDB Inc", | ||
"email": "[email protected]" | ||
}, | ||
"description": "MongoDB namespace parsing and validation", | ||
"main": "./index.js", | ||
"types": "./index.d.ts", | ||
"scripts": { | ||
"test": "mocha", | ||
"check": "mongodb-js-precommit" | ||
}, | ||
"homepage": "https://github.com/mongodb-js/devtools-shared", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/mongodb-js/ns.git" | ||
}, | ||
"keywords": [ | ||
"mongodb", | ||
"mongodb.js" | ||
], | ||
"license": "MIT", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"eslint-config-mongodb-js": "^5.0.3", | ||
"mocha": "^7.0.0", | ||
"mongodb-js-precommit": "^2.0.0" | ||
} | ||
} | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.