Comparing version 1.0.0 to 1.1.0
15
index.js
@@ -36,3 +36,9 @@ 'use strict'; | ||
if (typeof val === "string") { | ||
if (!/^[0-7]+$/.test(val)) { | ||
// check for octal string first | ||
if (val.charAt(0) === '0' && /^[0-7]+$/.test(val)) { | ||
val = parseInt(val, 8); | ||
} else if (val.charAt(0) !== '0' && /^[0-9]+$/.test(val)) { | ||
// legacy support for decimal strings | ||
val = parseInt(val, 10); | ||
} else { | ||
return cb(new Error(util.format("Expected octal string, got %j, defaulting to %j", | ||
@@ -42,4 +48,2 @@ val, defaultUmaskString)), | ||
} | ||
val = parseInt(val, 8); | ||
} else if (typeof val !== "number") { | ||
@@ -51,2 +55,4 @@ return cb(new Error(util.format("Expected number or octal string, got %j, defaulting to %j", | ||
val = Math.floor(val); | ||
if ((val < 0) || (val > 511)) { | ||
@@ -71,5 +77,2 @@ return cb(new Error(util.format("Must be in range 0..511 (0000..0777), got %j", val)), | ||
// if (err) log.warn("invalid umask", err.message) | ||
exports.toString = toString; | ||
@@ -76,0 +79,0 @@ exports.fromString = fromString; |
{ | ||
"name": "umask", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "convert umask from string <-> number", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# umask | ||
Convert umask from string <-> number. | ||
## Installation & Use | ||
``` | ||
$ npm install -S umask | ||
var umask = require('umask'); | ||
console.log(umask.toString(18)); // 0022 | ||
console.log(umask.fromString('0777')) // 511 | ||
``` | ||
## API | ||
### `toString( val )` | ||
Converts `val` to a 0-padded octal string. `val` is assumed to be a | ||
Number in the correct range (0..511) | ||
### `fromString( val, [cb] )` | ||
Converts `val` to a Number that can be used as a umask. `val` can | ||
be of the following forms: | ||
* String containing octal number (leading 0) | ||
* String containing decimal number | ||
* Number | ||
In all cases above, the value obtained is then converted to an integer and | ||
checked against the legal `umask` range 0..511 | ||
`fromString` can be used as a simple converter, with no error feedback, by | ||
omitting the optional callback argument `cb`: | ||
``` | ||
var mask = umask.fromString(val); | ||
// mask is now the umask descibed by val or | ||
// the default, 0022 (18 dec) | ||
``` | ||
The callback arguments are `(err, val)` where `err` is either `null` or an | ||
Error object and `val` is either the converted umask or the default umask, `0022`. | ||
``` | ||
umask.fromString(val, function (err, val) { | ||
if (err) { | ||
console.error("invalid umask: " + err.message) | ||
} | ||
/* do something with val */ | ||
}); | ||
``` | ||
The callback, if provided, is always called **synchronously**. | ||
### `validate( data, k, val )` | ||
This is a validation function of the form expected by `nopt`. If | ||
`val` is a valid umask, the function returns true and sets `data[k]`. | ||
If `val` is not a valid umask, the function returns false. | ||
The `validate` function is stricter than `fromString`: it only accepts | ||
Number or octal String values, and the String value must begin with `0`. | ||
The `validate` function does **not** accept Strings containing decimal | ||
numbers. | ||
# Maintainer | ||
Sam Mikes <smikes@cubane.com> | ||
# License | ||
MIT |
@@ -99,5 +99,11 @@ 'use strict'; | ||
expect(umask.fromString("0")).to.equal(0); | ||
expect(umask.fromString("010")).to.equal(8); | ||
expect(umask.fromString("0777")).to.equal(511); | ||
expect(umask.fromString("0024")).to.equal(20); | ||
expect(umask.fromString("8")).to.equal(8); | ||
expect(umask.fromString("9")).to.equal(9); | ||
expect(umask.fromString("18")).to.equal(18); | ||
expect(umask.fromString("16")).to.equal(16); | ||
expect(umask.fromString(0)).to.equal(0); | ||
@@ -108,2 +114,5 @@ expect(umask.fromString(20)).to.equal(20); | ||
expect(umask.fromString(0.1)).to.equal(0); | ||
expect(umask.fromString(511.1)).to.equal(511); | ||
done(); | ||
@@ -120,2 +129,10 @@ }); | ||
it('errors on invalid octal string', function (done) { | ||
umask.fromString("099", function (err, val) { | ||
expect(err.message).to.equal('Expected octal string, got "099", defaulting to "0022"'); | ||
expect(val).to.equal(18); | ||
done(); | ||
}); | ||
}); | ||
it('errors when non-string, non-number (boolean)', function (done) { | ||
@@ -122,0 +139,0 @@ umask.fromString(false, function (err, val) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13705
243398
10
196
78