node-opcua-numeric-range
Advanced tools
Comparing version 0.1.1-0 to 0.2.0
{ | ||
"name": "node-opcua-numeric-range", | ||
"version": "0.1.1-0", | ||
"version": "0.2.0", | ||
"description": "pure nodejs OPCUA SDK - module -numeric-range", | ||
@@ -12,15 +12,15 @@ "main": "src/numeric_range.js", | ||
"dependencies": { | ||
"node-opcua-assert": "^0.1.1-0", | ||
"node-opcua-basic-types": "^0.1.1-0", | ||
"node-opcua-enum": "^0.1.1-0", | ||
"node-opcua-factory": "^0.1.1-0", | ||
"node-opcua-status-code": "^0.1.1-0", | ||
"node-opcua-assert": "^0.2.0", | ||
"node-opcua-basic-types": "^0.2.0", | ||
"node-opcua-enum": "^0.2.0", | ||
"node-opcua-factory": "^0.2.0", | ||
"node-opcua-status-code": "^0.2.0", | ||
"underscore": "^1.8.3" | ||
}, | ||
"devDependencies": { | ||
"node-opcua-generator": "^0.1.1-0", | ||
"node-opcua-nodeid": "^0.1.1-0", | ||
"node-opcua-packet-analyzer": "^0.1.1-0", | ||
"node-opcua-test-helpers": "^0.1.1-0", | ||
"should": "13.2.0" | ||
"node-opcua-generator": "^0.2.0", | ||
"node-opcua-nodeid": "^0.2.0", | ||
"node-opcua-packet-analyzer": "^0.2.0", | ||
"node-opcua-test-helpers": "^0.2.0", | ||
"should": "13.2.1" | ||
}, | ||
@@ -27,0 +27,0 @@ "repository": { |
@@ -66,2 +66,3 @@ /* global NumericRange*/ | ||
coerce: function (value) { | ||
@@ -146,3 +147,3 @@ if (value instanceof NumericRange) { | ||
// detect multi dim range | ||
/* detect multi dim range*/ | ||
var values = str.split(","); | ||
@@ -159,3 +160,3 @@ | ||
colRange = elements[1]; | ||
if (rowRange === NumericRangeType.InvalidRange || colRange === NumericRangeType.InvalidRange) { | ||
if (rowRange.type === NumericRangeType.InvalidRange || colRange.type === NumericRangeType.InvalidRange) { | ||
return {type: NumericRangeType.InvalidRange, value: str}; | ||
@@ -270,5 +271,10 @@ } | ||
if (this.type === NumericRangeType.MatrixRange) { | ||
assert(_.isNumber(this.value[0][0])); | ||
assert(_.isNumber(this.value[0][1])); | ||
assert(_.isNumber(this.value[1][0])); | ||
assert(_.isNumber(this.value[1][1])); | ||
return _valid_range(this.value[0][0], this.value[0][1]) && | ||
_valid_range(this.value[1][0], this.value[1][1]); | ||
} else if (this.type === NumericRangeType.ArrayRange) { | ||
@@ -365,5 +371,6 @@ return _valid_range(this.value[0], this.value[1]); | ||
function extract_empty(array) { | ||
function extract_empty(array, dimensions) { | ||
return { | ||
array: slice(array, 0, array.length), | ||
dimensions: dimensions, | ||
statusCode: StatusCodes.Good | ||
@@ -383,2 +390,3 @@ }; | ||
function extract_array_range(array, low_index, high_index) { | ||
assert(_.isFinite(low_index) && _.isFinite(high_index)); | ||
assert(low_index >= 0); | ||
@@ -402,5 +410,5 @@ assert(low_index <= high_index); | ||
function extract_matrix_range(array, rowRange, colRange) { | ||
if (array.length === 0 || !isArrayLike(array[0])) { | ||
function extract_matrix_range(array, rowRange, colRange, dimension) { | ||
assert(_.isArray(rowRange) && _.isArray(colRange)); | ||
if (array.length === 0) { | ||
return { | ||
@@ -411,12 +419,64 @@ array: [], | ||
} | ||
var result = extract_array_range(array, rowRange[0], rowRange[1]); | ||
if (isArrayLike(array[0]) && !dimension) { | ||
// like extracting data from a one dimensionnal array of strings or byteStrings... | ||
var result = extract_array_range(array, rowRange[0], rowRange[1]); | ||
for (var i = 0; i < result.array.length; i++) { | ||
var e = result.array[i]; | ||
result.array[i] = extract_array_range(e, colRange[0], colRange[1]).array; | ||
} | ||
return result; | ||
} | ||
if (!dimension) { | ||
return { | ||
array: [], | ||
statusCode: StatusCodes.BadIndexRangeNoData | ||
}; | ||
} | ||
for (var i = 0; i < result.array.length; i++) { | ||
var e = result.array[i]; | ||
result.array[i] = extract_array_range(e, colRange[0], colRange[1]).array; | ||
assert(dimension, "expecting dimension to know the shape of the matrix represented by the flat array"); | ||
// | ||
var rowLow = rowRange[0]; | ||
var rowHigh = rowRange[1]; | ||
var colLow = colRange[0]; | ||
var colHigh = colRange[1]; | ||
var nbRow = dimension[0]; | ||
var nbCol = dimension[1]; | ||
var nbRowDest = rowHigh - rowLow + 1; | ||
var nbColDest = colHigh - colLow + 1; | ||
// constrruct an array of the same type with the appropriate length to | ||
// store the extracted matrix. | ||
var tmp = new array.constructor(nbColDest * nbRowDest); | ||
var row, col, r, c; | ||
r = 0; | ||
for (row = rowLow; row <= rowHigh; row++) { | ||
c = 0; | ||
for (col = colLow; col <= colHigh; col++) { | ||
var srcIndex = row * nbCol + col; | ||
var destIndex = r * nbColDest + c; | ||
tmp[destIndex] = array[srcIndex]; | ||
c++; | ||
} | ||
r += 1; | ||
} | ||
return result; | ||
return { | ||
array: tmp, | ||
dimensions: [nbRowDest, nbColDest], | ||
statusCode: StatusCodes.Good | ||
}; | ||
} | ||
NumericRange.prototype.extract_values = function (array) { | ||
/** | ||
* | ||
* @param array {Array<Any>} flat array containing values | ||
* @param [dimensions = null ]{Array<Number>} dimension of the matrix if data is a matrix | ||
* @returns {*} | ||
*/ | ||
NumericRange.prototype.extract_values = function (array, dimensions) { | ||
@@ -431,3 +491,3 @@ if (!array) { | ||
case NumericRangeType.Empty: | ||
return extract_empty(array); | ||
return extract_empty(array, dimensions); | ||
@@ -446,3 +506,3 @@ case NumericRangeType.SingleValue: | ||
var colRange = this.value[1]; | ||
return extract_matrix_range(array, rowRange, colRange); | ||
return extract_matrix_range(array, rowRange, colRange, dimensions); | ||
@@ -455,3 +515,3 @@ default: | ||
function assert_array_or_buffer(array) { | ||
assert(_.isArray(array) || (array.buffer instanceof ArrayBuffer ) || array instanceof Buffer); | ||
assert(_.isArray(array) || (array.buffer instanceof ArrayBuffer) || array instanceof Buffer); | ||
} | ||
@@ -517,3 +577,3 @@ | ||
} | ||
if ((this.type !== NumericRangeType.Empty ) && newValues.length !== (high_index - low_index + 1)) { | ||
if ((this.type !== NumericRangeType.Empty) && newValues.length !== (high_index - low_index + 1)) { | ||
return {array: [], statusCode: StatusCodes.BadIndexRangeInvalid}; | ||
@@ -520,0 +580,0 @@ } |
@@ -10,4 +10,2 @@ /*global describe,require,it */ | ||
describe("Testing numerical range", function () { | ||
@@ -28,3 +26,3 @@ | ||
it("should construct a NumericRange from a integer", function () { | ||
it("should construct a NumericRange from a integer (InvalidRange)", function () { | ||
var nr = new NumericRange("-12"); | ||
@@ -114,3 +112,9 @@ nr.type.should.eql(NumericRangeType.InvalidRange); | ||
}); | ||
it("should be an InvalidRange when constructed with a string with invalid array range (low==high) ", function () { | ||
var nr = new NumericRange([12,12]); | ||
nr.type.should.equal(NumericRange.NumericRangeType.InvalidRange); | ||
nr.isValid().should.equal(false); | ||
}); | ||
it("should be an InvalidRange when constructed with a string with invalid array range ( low > high )", function () { | ||
@@ -122,3 +126,2 @@ var nr = new NumericRange("15:12"); | ||
it("should be an InvalidRange when constructed with a badly formed string '2-4' ", function () { | ||
@@ -138,3 +141,3 @@ var nr = new NumericRange("2-4"); | ||
it("should be an MatrixRange when constructed with a matrix formed string : '1:3,4:5' ", function () { | ||
it("should be an MatrixRange when constructed with a string : '1:3,4:5' ", function () { | ||
var nr = new NumericRange("1:3,4:5"); | ||
@@ -145,3 +148,21 @@ nr.type.should.equal(NumericRange.NumericRangeType.MatrixRange); | ||
it("should be an MatrixRange when constructed with a matrix formed string : '1,2' ", function () { | ||
it("should be an InvalidRange when constructed with a matrix form string : '1:1,2:2'",function() { | ||
var nr = new NumericRange("1:1,2:2"); | ||
nr.type.should.equal(NumericRange.NumericRangeType.InvalidRange); | ||
nr.isValid().should.equal(false); | ||
}); | ||
it("should be an InvalidRange when constructed with a matrix form string : '1,2:2'",function() { | ||
var nr = new NumericRange("1,2:2"); | ||
nr.type.should.equal(NumericRange.NumericRangeType.InvalidRange); | ||
nr.isValid().should.equal(false); | ||
}); | ||
it("should be an InvalidRange when constructed with a matrix form string : '1:1,2'",function() { | ||
var nr = new NumericRange("1:1,2"); | ||
nr.type.should.equal(NumericRange.NumericRangeType.InvalidRange); | ||
nr.isValid().should.equal(false); | ||
}); | ||
it("should be an MatrixRange when constructed with a matrix form string : '1,2' ", function () { | ||
var nr = new NumericRange("1,2"); | ||
@@ -168,2 +189,3 @@ nr.type.should.equal(NumericRange.NumericRangeType.MatrixRange); | ||
}); | ||
@@ -334,3 +356,76 @@ | ||
}); | ||
describe("extracting ranges from matrix", function () { | ||
function createMatrix(row,col,flatarray) { | ||
if (!(flatarray instanceof Array && row*col == flatarray.length)) { | ||
throw new Error("Invalid Matrix Size"); | ||
} | ||
return flatarray; | ||
var array = []; | ||
for (var i=0;i<row;i++) { | ||
array[i] = flatarray.slice(i*col,i*col+col); | ||
} | ||
return array; | ||
} | ||
var matrix = createMatrix(3,3,[11, 12, 13, 21, 22, 23, 31, 32, 33]); | ||
var dimensions = [3,3]; | ||
beforeEach(function () { | ||
matrix.length.should.eql(9); | ||
matrix.should.eql([11,12,13,21,22,23,31,32,33]); | ||
}); | ||
afterEach(function () { | ||
matrix.length.should.eql(9,"original array should not be affected"); | ||
matrix.should.eql([11,12,13,21,22,23,31,32,33]); | ||
}); | ||
it("should extract sub matrix at 0,0", function () { | ||
var nr = new NumericRange("0,0"); | ||
var r = nr.extract_values(matrix,dimensions); | ||
r.statusCode.should.eql(StatusCodes.Good); | ||
r.dimensions.should.eql([1,1]); | ||
r.array.length.should.eql(1); | ||
r.array[0].should.eql(11); | ||
}); | ||
it("should extract sub matrix at 1,0", function () { | ||
var nr = new NumericRange("1,0"); | ||
var r = nr.extract_values(matrix,dimensions); | ||
r.statusCode.should.eql(StatusCodes.Good); | ||
r.dimensions.should.eql([1,1]); | ||
r.array.length.should.eql(1); | ||
r.array[0].should.eql(21); | ||
}); | ||
it("should extract sub matrix at 0,1", function () { | ||
var nr = new NumericRange("0,1"); | ||
var r = nr.extract_values(matrix,dimensions); | ||
r.statusCode.should.eql(StatusCodes.Good); | ||
r.dimensions.should.eql([1,1]); | ||
r.array.length.should.eql(1); | ||
r.array[0].should.eql(12); | ||
}); | ||
it("should extract sub matrix column at 0:2,1 ( a column)", function () { | ||
var nr = new NumericRange("0:2,0"); | ||
var r = nr.extract_values(matrix,dimensions); | ||
r.statusCode.should.eql(StatusCodes.Good); | ||
r.array.length.should.eql(3); | ||
r.dimensions.should.eql([3,1]); | ||
r.array.length.should.eql(3); | ||
r.array[0].should.eql(11); | ||
r.array[1].should.eql(21); | ||
r.array[2].should.eql(31); | ||
}); | ||
it("should extract sub matrix row at 0:2,1 ( a row)", function () { | ||
var nr = new NumericRange("0,0:2"); | ||
var r = nr.extract_values(matrix,dimensions); | ||
r.statusCode.should.eql(StatusCodes.Good); | ||
r.dimensions.should.eql([1,3]); | ||
r.array.length.should.eql(3); | ||
r.array[0].should.eql(11); | ||
r.array[1].should.eql(12); | ||
r.array[2].should.eql(13); | ||
}); | ||
}); | ||
function makeBuffer(values) { | ||
@@ -399,3 +494,3 @@ var buff = new Buffer(values.length); | ||
it(name + " Z7 - it should return BadIndexRangeNoData if range is a MatrixRange and value is an array", function () { | ||
it(name + " Z7 - it should return BadIndexRangeNoData if range is a MatrixRange and value is an array that contains no ArrayLike Elements", function () { | ||
var nr = new NumericRange("1,1"); | ||
@@ -602,4 +697,2 @@ nr.type.should.eql(NumericRange.NumericRangeType.MatrixRange); | ||
describe("Operations", function () { | ||
@@ -645,3 +738,2 @@ | ||
}); | ||
@@ -648,0 +740,0 @@ |
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
59096
1190
+ Addednode-opcua-assert@0.2.0(transitive)
+ Addednode-opcua-basic-types@0.2.3(transitive)
+ Addednode-opcua-binary-stream@0.2.3(transitive)
+ Addednode-opcua-buffer-utils@0.2.0(transitive)
+ Addednode-opcua-constants@0.2.0(transitive)
+ Addednode-opcua-date-time@0.2.3(transitive)
+ Addednode-opcua-debug@0.2.2(transitive)
+ Addednode-opcua-enum@0.2.3(transitive)
+ Addednode-opcua-factory@0.2.3(transitive)
+ Addednode-opcua-guid@0.2.0(transitive)
+ Addednode-opcua-nodeid@0.2.3(transitive)
+ Addednode-opcua-status-code@0.2.3(transitive)
+ Addednode-opcua-utils@0.2.3(transitive)
- Removednode-opcua-assert@0.1.1-0(transitive)
- Removednode-opcua-basic-types@0.1.1-0(transitive)
- Removednode-opcua-binary-stream@0.1.1-0(transitive)
- Removednode-opcua-buffer-utils@0.1.1-0(transitive)
- Removednode-opcua-constants@0.1.1-0(transitive)
- Removednode-opcua-date-time@0.1.1-0(transitive)
- Removednode-opcua-debug@0.1.1-0(transitive)
- Removednode-opcua-enum@0.1.1-0(transitive)
- Removednode-opcua-factory@0.1.1-0(transitive)
- Removednode-opcua-guid@0.1.1-0(transitive)
- Removednode-opcua-nodeid@0.1.1-0(transitive)
- Removednode-opcua-status-code@0.1.1-0(transitive)
- Removednode-opcua-utils@0.1.1-0(transitive)
Updatednode-opcua-assert@^0.2.0
Updatednode-opcua-enum@^0.2.0
Updatednode-opcua-factory@^0.2.0