aws-dynamodb-auto-marshaller
Advanced tools
Comparing version 0.7.3-pull-request-167-3 to 0.7.3-pull-request-167-4
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var ObjectSet_1 = require("./ObjectSet"); | ||
const ObjectSet_1 = require("./ObjectSet"); | ||
/** | ||
@@ -10,16 +9,12 @@ * A set of binary values represented as either ArrayBuffer objects or | ||
*/ | ||
var BinarySet = /** @class */ (function (_super) { | ||
tslib_1.__extends(BinarySet, _super); | ||
function BinarySet() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
BinarySet.prototype.delete = function (value) { | ||
var valueView = getBinaryView(value); | ||
var scrubbedValues = this._values.filter(function (item) { | ||
class BinarySet extends ObjectSet_1.ObjectSet { | ||
delete(value) { | ||
const valueView = getBinaryView(value); | ||
const scrubbedValues = this._values.filter(item => { | ||
return !binaryEquals(getBinaryView(item), valueView); | ||
}); | ||
var numRemoved = this._values.length - scrubbedValues.length; | ||
const numRemoved = this._values.length - scrubbedValues.length; | ||
this._values = scrubbedValues; | ||
return numRemoved > 0; | ||
}; | ||
} | ||
/** | ||
@@ -39,24 +34,12 @@ * @inheritDoc | ||
*/ | ||
BinarySet.prototype.has = function (value) { | ||
var e_1, _a; | ||
var valueView = getBinaryView(value); | ||
try { | ||
for (var _b = tslib_1.__values(this), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var item = _c.value; | ||
if (binaryEquals(getBinaryView(item), valueView)) { | ||
return true; | ||
} | ||
has(value) { | ||
const valueView = getBinaryView(value); | ||
for (let item of this) { | ||
if (binaryEquals(getBinaryView(item), valueView)) { | ||
return true; | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return false; | ||
}; | ||
return BinarySet; | ||
}(ObjectSet_1.ObjectSet)); | ||
} | ||
} | ||
exports.BinarySet = BinarySet; | ||
@@ -67,3 +50,3 @@ function binaryEquals(a, b) { | ||
} | ||
for (var i = 0; i < a.byteLength; i++) { | ||
for (let i = 0; i < a.byteLength; i++) { | ||
if (a.getUint8(i) !== b.getUint8(i)) { | ||
@@ -70,0 +53,0 @@ return false; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./BinarySet"), exports); | ||
@@ -5,0 +5,0 @@ tslib_1.__exportStar(require("./Marshaller"), exports); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var BinarySet_1 = require("./BinarySet"); | ||
var isArrayBuffer_1 = require("./isArrayBuffer"); | ||
var NumberValue_1 = require("./NumberValue"); | ||
var NumberValueSet_1 = require("./NumberValueSet"); | ||
const BinarySet_1 = require("./BinarySet"); | ||
const isArrayBuffer_1 = require("./isArrayBuffer"); | ||
const NumberValue_1 = require("./NumberValue"); | ||
const NumberValueSet_1 = require("./NumberValueSet"); | ||
exports.EmptyHandlingStrategies = { | ||
@@ -27,5 +26,4 @@ omit: 'omit', | ||
*/ | ||
var Marshaller = /** @class */ (function () { | ||
function Marshaller(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.onEmpty, onEmpty = _c === void 0 ? 'leave' : _c, _d = _b.onInvalid, onInvalid = _d === void 0 ? 'throw' : _d, _e = _b.unwrapNumbers, unwrapNumbers = _e === void 0 ? false : _e; | ||
class Marshaller { | ||
constructor({ onEmpty = 'leave', onInvalid = 'throw', unwrapNumbers = false } = {}) { | ||
this.onEmpty = onEmpty; | ||
@@ -39,9 +37,9 @@ this.onInvalid = onInvalid; | ||
*/ | ||
Marshaller.prototype.marshallItem = function (item) { | ||
var value = this.marshallValue(item); | ||
marshallItem(item) { | ||
const value = this.marshallValue(item); | ||
if (!(value && value.M) && this.onInvalid === 'throw') { | ||
throw new Error("Cannot serialize " + typeof item + " as an attribute map"); | ||
throw new Error(`Cannot serialize ${typeof item} as an attribute map`); | ||
} | ||
return value && value.M ? value.M : {}; | ||
}; | ||
} | ||
/** | ||
@@ -53,3 +51,3 @@ * Convert a JavaScript value into a DynamoDB AttributeValue or `undefined`. | ||
*/ | ||
Marshaller.prototype.marshallValue = function (value) { | ||
marshallValue(value) { | ||
switch (typeof value) { | ||
@@ -70,6 +68,6 @@ case 'boolean': | ||
if (this.onInvalid === 'throw') { | ||
throw new Error("Cannot serialize values of the " + typeof value + " type"); | ||
throw new Error(`Cannot serialize values of the ${typeof value} type`); | ||
} | ||
} | ||
}; | ||
} | ||
/** | ||
@@ -80,11 +78,9 @@ * Convert a DynamoDB operation result (an object with string keys and | ||
*/ | ||
Marshaller.prototype.unmarshallItem = function (item) { | ||
unmarshallItem(item) { | ||
return this.unmarshallValue({ M: item }); | ||
}; | ||
} | ||
/** | ||
* Convert a DynamoDB AttributeValue into a native JavaScript value. | ||
*/ | ||
Marshaller.prototype.unmarshallValue = function (item) { | ||
var e_1, _a, e_2, _b; | ||
var _this = this; | ||
unmarshallValue(item) { | ||
if (item.S !== undefined) { | ||
@@ -108,16 +104,6 @@ return item.S; | ||
if (item.SS !== undefined) { | ||
var set = new Set(); | ||
try { | ||
for (var _c = tslib_1.__values(item.SS), _d = _c.next(); !_d.done; _d = _c.next()) { | ||
var member = _d.value; | ||
set.add(member); | ||
} | ||
const set = new Set(); | ||
for (let member of item.SS) { | ||
set.add(member); | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_d && !_d.done && (_a = _c.return)) _a.call(_c); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return set; | ||
@@ -127,19 +113,9 @@ } | ||
if (this.unwrapNumbers) { | ||
var set = new Set(); | ||
try { | ||
for (var _e = tslib_1.__values(item.NS), _f = _e.next(); !_f.done; _f = _e.next()) { | ||
var member = _f.value; | ||
set.add(Number(member)); | ||
} | ||
const set = new Set(); | ||
for (let member of item.NS) { | ||
set.add(Number(member)); | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_f && !_f.done && (_b = _e.return)) _b.call(_e); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
return set; | ||
} | ||
return new NumberValueSet_1.NumberValueSet(item.NS.map(function (numberString) { return new NumberValue_1.NumberValue(numberString); })); | ||
return new NumberValueSet_1.NumberValueSet(item.NS.map(numberString => new NumberValue_1.NumberValue(numberString))); | ||
} | ||
@@ -152,9 +128,9 @@ if (item.BS !== undefined) { | ||
} | ||
var _g = item.M, M = _g === void 0 ? {} : _g; | ||
return Object.keys(M).reduce(function (map, key) { | ||
map[key] = _this.unmarshallValue(M[key]); | ||
const { M = {} } = item; | ||
return Object.keys(M).reduce((map, key) => { | ||
map[key] = this.unmarshallValue(M[key]); | ||
return map; | ||
}, {}); | ||
}; | ||
Marshaller.prototype.marshallComplexType = function (value) { | ||
} | ||
marshallComplexType(value) { | ||
if (value === null) { | ||
@@ -179,4 +155,4 @@ return { NULL: true }; | ||
return this.marshallObject(value); | ||
}; | ||
Marshaller.prototype.marshallBinaryValue = function (binary) { | ||
} | ||
marshallBinaryValue(binary) { | ||
if (binary.byteLength > 0 || this.onEmpty === 'leave') { | ||
@@ -188,56 +164,33 @@ return { B: binary }; | ||
} | ||
}; | ||
Marshaller.prototype.marshallList = function (list) { | ||
var e_3, _a; | ||
var values = []; | ||
try { | ||
for (var list_1 = tslib_1.__values(list), list_1_1 = list_1.next(); !list_1_1.done; list_1_1 = list_1.next()) { | ||
var value = list_1_1.value; | ||
var marshalled = this.marshallValue(value); | ||
if (marshalled) { | ||
values.push(marshalled); | ||
} | ||
} | ||
marshallList(list) { | ||
const values = []; | ||
for (let value of list) { | ||
const marshalled = this.marshallValue(value); | ||
if (marshalled) { | ||
values.push(marshalled); | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (list_1_1 && !list_1_1.done && (_a = list_1.return)) _a.call(list_1); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
return { L: values }; | ||
}; | ||
Marshaller.prototype.marshallMap = function (map) { | ||
var e_4, _a; | ||
var members = {}; | ||
try { | ||
for (var map_1 = tslib_1.__values(map), map_1_1 = map_1.next(); !map_1_1.done; map_1_1 = map_1.next()) { | ||
var _b = tslib_1.__read(map_1_1.value, 2), key = _b[0], value = _b[1]; | ||
if (typeof key !== 'string') { | ||
if (this.onInvalid === 'omit') { | ||
continue; | ||
} | ||
throw new Error("MapAttributeValues must have strings as keys; " + typeof key + " received instead"); | ||
} | ||
marshallMap(map) { | ||
const members = {}; | ||
for (let [key, value] of map) { | ||
if (typeof key !== 'string') { | ||
if (this.onInvalid === 'omit') { | ||
continue; | ||
} | ||
var marshalled = this.marshallValue(value); | ||
if (marshalled) { | ||
members[key] = marshalled; | ||
} | ||
throw new Error(`MapAttributeValues must have strings as keys; ${typeof key} received instead`); | ||
} | ||
} | ||
catch (e_4_1) { e_4 = { error: e_4_1 }; } | ||
finally { | ||
try { | ||
if (map_1_1 && !map_1_1.done && (_a = map_1.return)) _a.call(map_1); | ||
const marshalled = this.marshallValue(value); | ||
if (marshalled) { | ||
members[key] = marshalled; | ||
} | ||
finally { if (e_4) throw e_4.error; } | ||
} | ||
return { M: members }; | ||
}; | ||
Marshaller.prototype.marshallObject = function (object) { | ||
var _this = this; | ||
} | ||
marshallObject(object) { | ||
return { | ||
M: Object.keys(object).reduce(function (map, key) { | ||
var marshalled = _this.marshallValue(object[key]); | ||
M: Object.keys(object).reduce((map, key) => { | ||
const marshalled = this.marshallValue(object[key]); | ||
if (marshalled) { | ||
@@ -249,4 +202,4 @@ map[key] = marshalled; | ||
}; | ||
}; | ||
Marshaller.prototype.marshallSet = function (arg) { | ||
} | ||
marshallSet(arg) { | ||
switch (getSetType(arg[Symbol.iterator]().next().value)) { | ||
@@ -270,30 +223,19 @@ case 'binary': | ||
} | ||
}; | ||
Marshaller.prototype.collectSet = function (set, isEmpty, tag, elementType, transform) { | ||
var e_5, _a, _b; | ||
var values = []; | ||
try { | ||
for (var set_1 = tslib_1.__values(set), set_1_1 = set_1.next(); !set_1_1.done; set_1_1 = set_1.next()) { | ||
var element = set_1_1.value; | ||
if (getSetType(element) !== elementType) { | ||
if (this.onInvalid === 'omit') { | ||
continue; | ||
} | ||
throw new Error("Unable to serialize " + typeof element + " as a member of a " + elementType + " set"); | ||
} | ||
collectSet(set, isEmpty, tag, elementType, transform) { | ||
const values = []; | ||
for (let element of set) { | ||
if (getSetType(element) !== elementType) { | ||
if (this.onInvalid === 'omit') { | ||
continue; | ||
} | ||
if (!isEmpty(element) || | ||
this.onEmpty === 'leave') { | ||
values.push(transform ? transform(element) : element); | ||
} | ||
throw new Error(`Unable to serialize ${typeof element} as a member of a ${elementType} set`); | ||
} | ||
} | ||
catch (e_5_1) { e_5 = { error: e_5_1 }; } | ||
finally { | ||
try { | ||
if (set_1_1 && !set_1_1.done && (_a = set_1.return)) _a.call(set_1); | ||
if (!isEmpty(element) || | ||
this.onEmpty === 'leave') { | ||
values.push(transform ? transform(element) : element); | ||
} | ||
finally { if (e_5) throw e_5.error; } | ||
} | ||
if (values.length > 0 || this.onEmpty === 'leave') { | ||
return _b = {}, _b[tag] = values, _b; | ||
return { [tag]: values }; | ||
} | ||
@@ -303,4 +245,4 @@ if (this.onEmpty === 'nullify') { | ||
} | ||
}; | ||
Marshaller.prototype.handleEmptyString = function (value) { | ||
} | ||
handleEmptyString(value) { | ||
switch (this.onEmpty) { | ||
@@ -312,8 +254,7 @@ case 'leave': | ||
} | ||
}; | ||
return Marshaller; | ||
}()); | ||
} | ||
} | ||
exports.Marshaller = Marshaller; | ||
function getSetType(arg) { | ||
var type = typeof arg; | ||
const type = typeof arg; | ||
if (type === 'string' || type === 'number' || type === 'undefined') { | ||
@@ -320,0 +261,0 @@ return type; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var NUMBER_VALUE_TAG = 'DynamoDbNumberValue'; | ||
var EXPECTED_TAG = "[object " + NUMBER_VALUE_TAG + "]"; | ||
const NUMBER_VALUE_TAG = 'DynamoDbNumberValue'; | ||
const EXPECTED_TAG = `[object ${NUMBER_VALUE_TAG}]`; | ||
/** | ||
@@ -11,4 +11,4 @@ * A number that may contain greater precision than can safely be stored in | ||
*/ | ||
var NumberValue = /** @class */ (function () { | ||
function NumberValue(value) { | ||
class NumberValue { | ||
constructor(value) { | ||
this[Symbol.toStringTag] = NUMBER_VALUE_TAG; | ||
@@ -21,5 +21,5 @@ this.value = value.toString().trim(); | ||
*/ | ||
NumberValue.prototype.toJSON = function () { | ||
toJSON() { | ||
return this.valueOf(); | ||
}; | ||
} | ||
/** | ||
@@ -29,5 +29,5 @@ * Convert the value to its desired string representation. Called | ||
*/ | ||
NumberValue.prototype.toString = function () { | ||
toString() { | ||
return this.value; | ||
}; | ||
} | ||
/** | ||
@@ -37,14 +37,13 @@ * Convert the value to its desired literal representation. Called | ||
*/ | ||
NumberValue.prototype.valueOf = function () { | ||
valueOf() { | ||
return Number(this.value); | ||
}; | ||
} | ||
/** | ||
* Evaluate whether the provided value is a NumberValue object. | ||
*/ | ||
NumberValue.isNumberValue = function (arg) { | ||
static isNumberValue(arg) { | ||
return (typeof NumberValue === 'function' && arg instanceof NumberValue) | ||
|| Object.prototype.toString.call(arg) === EXPECTED_TAG; | ||
}; | ||
return NumberValue; | ||
}()); | ||
} | ||
} | ||
exports.NumberValue = NumberValue; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var ObjectSet_1 = require("./ObjectSet"); | ||
var NumberValue_1 = require("./NumberValue"); | ||
const ObjectSet_1 = require("./ObjectSet"); | ||
const NumberValue_1 = require("./NumberValue"); | ||
/** | ||
@@ -11,7 +10,3 @@ * A set of numeric values represented internally as NumberValue objects. | ||
*/ | ||
var NumberValueSet = /** @class */ (function (_super) { | ||
tslib_1.__extends(NumberValueSet, _super); | ||
function NumberValueSet() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
class NumberValueSet extends ObjectSet_1.ObjectSet { | ||
/** | ||
@@ -23,39 +18,27 @@ * @inheritDoc | ||
*/ | ||
NumberValueSet.prototype.add = function (value) { | ||
add(value) { | ||
if (typeof value === 'number' || typeof value === 'string') { | ||
value = new NumberValue_1.NumberValue(value); | ||
} | ||
_super.prototype.add.call(this, value); | ||
super.add(value); | ||
return this; | ||
}; | ||
NumberValueSet.prototype.delete = function (value) { | ||
var valueString = value.toString(); | ||
var scrubbedValues = this._values | ||
.filter(function (item) { return item.toString() !== valueString; }); | ||
var numRemoved = this._values.length - scrubbedValues.length; | ||
} | ||
delete(value) { | ||
const valueString = value.toString(); | ||
const scrubbedValues = this._values | ||
.filter(item => item.toString() !== valueString); | ||
const numRemoved = this._values.length - scrubbedValues.length; | ||
this._values = scrubbedValues; | ||
return numRemoved > 0; | ||
}; | ||
NumberValueSet.prototype.has = function (value) { | ||
var e_1, _a; | ||
var valueString = value.toString(); | ||
try { | ||
for (var _b = tslib_1.__values(this), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var item = _c.value; | ||
if (item.toString() === valueString) { | ||
return true; | ||
} | ||
} | ||
has(value) { | ||
const valueString = value.toString(); | ||
for (let item of this) { | ||
if (item.toString() === valueString) { | ||
return true; | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return false; | ||
}; | ||
return NumberValueSet; | ||
}(ObjectSet_1.ObjectSet)); | ||
} | ||
} | ||
exports.NumberValueSet = NumberValueSet; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var tslib_1 = require("tslib"); | ||
var ObjectSet = /** @class */ (function () { | ||
class ObjectSet { | ||
/** | ||
@@ -10,4 +9,3 @@ * Creates a new ObjectSet and optionally seeds it with values. | ||
*/ | ||
function ObjectSet(iterable) { | ||
var e_1, _a; | ||
constructor(iterable) { | ||
/** | ||
@@ -20,15 +18,5 @@ * Returns the string literal 'Set' for use by Object.prototype.toString. | ||
if (iterable) { | ||
try { | ||
for (var iterable_1 = tslib_1.__values(iterable), iterable_1_1 = iterable_1.next(); !iterable_1_1.done; iterable_1_1 = iterable_1.next()) { | ||
var item = iterable_1_1.value; | ||
this.add(item); | ||
} | ||
for (let item of iterable) { | ||
this.add(item); | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (iterable_1_1 && !iterable_1_1.done && (_a = iterable_1.return)) _a.call(iterable_1); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
} | ||
@@ -42,3 +30,3 @@ } | ||
*/ | ||
ObjectSet.prototype.add = function (value) { | ||
add(value) { | ||
if (!this.has(value)) { | ||
@@ -48,9 +36,9 @@ this._values.push(value); | ||
return this; | ||
}; | ||
} | ||
/** | ||
* Remove all values from the set. | ||
*/ | ||
ObjectSet.prototype.clear = function () { | ||
clear() { | ||
this._values = []; | ||
}; | ||
} | ||
/** | ||
@@ -62,5 +50,5 @@ * Returns an iterable two-member tuples for each item in the set, where | ||
*/ | ||
ObjectSet.prototype.entries = function () { | ||
return this._values.map(function (value) { return [value, value]; })[Symbol.iterator](); | ||
}; | ||
entries() { | ||
return this._values.map(value => [value, value])[Symbol.iterator](); | ||
} | ||
/** | ||
@@ -72,38 +60,32 @@ * Invokes a callback once for each member of the set. | ||
*/ | ||
ObjectSet.prototype.forEach = function (callback, thisArg) { | ||
var _this = this; | ||
this._values.forEach(function (value, index, array) { | ||
callback.call(thisArg, value, value, _this); | ||
forEach(callback, thisArg) { | ||
this._values.forEach((value, index, array) => { | ||
callback.call(thisArg, value, value, this); | ||
}, thisArg); | ||
}; | ||
} | ||
/** | ||
* Returns an IterableIterator of each member of the set. | ||
*/ | ||
ObjectSet.prototype.keys = function () { | ||
keys() { | ||
return this[Symbol.iterator](); | ||
}; | ||
Object.defineProperty(ObjectSet.prototype, "size", { | ||
/** | ||
* Returns the number of members in the set. | ||
*/ | ||
get: function () { | ||
return this._values.length; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
} | ||
/** | ||
* Returns the number of members in the set. | ||
*/ | ||
get size() { | ||
return this._values.length; | ||
} | ||
/** | ||
* Returns an IterableIterator of each member of the set. | ||
*/ | ||
ObjectSet.prototype.values = function () { | ||
values() { | ||
return this[Symbol.iterator](); | ||
}; | ||
} | ||
/** | ||
* Returns an IterableIterator of each member of the set. | ||
*/ | ||
ObjectSet.prototype[Symbol.iterator] = function () { | ||
[Symbol.iterator]() { | ||
return this._values[Symbol.iterator](); | ||
}; | ||
return ObjectSet; | ||
}()); | ||
} | ||
} | ||
exports.ObjectSet = ObjectSet; |
{ | ||
"name": "aws-dynamodb-auto-marshaller", | ||
"version": "0.7.3-pull-request-167-3", | ||
"version": "0.7.3-pull-request-167-4", | ||
"description": "A data marshaller that converts JavaScript types into Amazon DynamoDB AttributeValues", | ||
@@ -44,3 +44,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "7cfb733e819ee0a5a3cfc7ee8f1f4222f0ac7954" | ||
"gitHead": "78e82a3c68facefa4d2daebc382340b99ac72954" | ||
} |
45209
800