Comparing version 1.7.5 to 1.8.0
@@ -21,1 +21,4 @@ export declare const isUndefined: (payload: any) => payload is undefined; | ||
export declare const isInfinite: (payload: any) => payload is number; | ||
export declare type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor; | ||
export declare type TypedArray = InstanceType<TypedArrayConstructor>; | ||
export declare const isTypedArray: (payload: any) => payload is TypedArray; |
@@ -66,2 +66,5 @@ var getType = function (payload) { | ||
}; | ||
export var isTypedArray = function (payload) { | ||
return ArrayBuffer.isView(payload) && !(payload instanceof DataView); | ||
}; | ||
//# sourceMappingURL=is.js.map |
export declare type PrimitiveTypeAnnotation = 'number' | 'undefined' | 'bigint'; | ||
declare type LeafTypeAnnotation = PrimitiveTypeAnnotation | 'regexp' | 'Date' | 'Error'; | ||
declare type TypedArrayAnnotation = ['typed-array', string]; | ||
declare type ClassTypeAnnotation = ['class', string]; | ||
@@ -7,3 +8,3 @@ declare type SymbolTypeAnnotation = ['symbol', string]; | ||
declare type SimpleTypeAnnotation = LeafTypeAnnotation | 'map' | 'set'; | ||
declare type CompositeTypeAnnotation = ClassTypeAnnotation | SymbolTypeAnnotation | CustomTypeAnnotation; | ||
declare type CompositeTypeAnnotation = TypedArrayAnnotation | ClassTypeAnnotation | SymbolTypeAnnotation | CustomTypeAnnotation; | ||
export declare type TypeAnnotation = SimpleTypeAnnotation | CompositeTypeAnnotation; | ||
@@ -10,0 +11,0 @@ export declare function isInstanceOfRegisteredClass(potentialClass: any): potentialClass is any; |
@@ -33,3 +33,3 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
import { isBigint, isDate, isInfinite, isMap, isNaNValue, isRegExp, isSet, isUndefined, isSymbol, isArray, isError, } from './is'; | ||
import { isBigint, isDate, isInfinite, isMap, isNaNValue, isRegExp, isSet, isUndefined, isSymbol, isArray, isError, isTypedArray, } from './is'; | ||
import { ClassRegistry } from './class-registry'; | ||
@@ -125,2 +125,23 @@ import { SymbolRegistry } from './symbol-registry'; | ||
}); | ||
var constructorToName = [ | ||
Int8Array, | ||
Uint8Array, | ||
Int16Array, | ||
Uint16Array, | ||
Int32Array, | ||
Uint32Array, | ||
Float32Array, | ||
Float64Array, | ||
Uint8ClampedArray, | ||
].reduce(function (obj, ctor) { | ||
obj[ctor.name] = ctor; | ||
return obj; | ||
}, {}); | ||
var typedArrayRule = compositeTransformation(isTypedArray, function (v) { return ['typed-array', v.constructor.name]; }, function (v) { return __spreadArray([], __read(v)); }, function (v, a) { | ||
var ctor = constructorToName[a[1]]; | ||
if (!ctor) { | ||
throw new Error('Trying to deserialize unknown typed array'); | ||
} | ||
return new ctor(v); | ||
}); | ||
export function isInstanceOfRegisteredClass(potentialClass) { | ||
@@ -168,3 +189,3 @@ if (potentialClass === null || potentialClass === void 0 ? void 0 : potentialClass.constructor) { | ||
}); | ||
var compositeRules = [classRule, symbolRule, customRule]; | ||
var compositeRules = [classRule, symbolRule, customRule, typedArrayRule]; | ||
export var transformValue = function (value) { | ||
@@ -204,2 +225,4 @@ var applicableCompositeRule = findArr(compositeRules, function (rule) { | ||
return customRule.untransform(json, type); | ||
case 'typed-array': | ||
return typedArrayRule.untransform(json, type); | ||
default: | ||
@@ -206,0 +229,0 @@ throw new Error('Unknown transformation: ' + type); |
@@ -21,1 +21,4 @@ export declare const isUndefined: (payload: any) => payload is undefined; | ||
export declare const isInfinite: (payload: any) => payload is number; | ||
export declare type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor; | ||
export declare type TypedArray = InstanceType<TypedArrayConstructor>; | ||
export declare const isTypedArray: (payload: any) => payload is TypedArray; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.isInfinite = exports.isBigint = exports.isPrimitive = exports.isNaNValue = exports.isError = exports.isDate = exports.isSymbol = exports.isSet = exports.isMap = exports.isRegExp = exports.isBoolean = exports.isNumber = exports.isString = exports.isArray = exports.isEmptyObject = exports.isPlainObject = exports.isNull = exports.isUndefined = void 0; | ||
exports.isTypedArray = exports.isInfinite = exports.isBigint = exports.isPrimitive = exports.isNaNValue = exports.isError = exports.isDate = exports.isSymbol = exports.isSet = exports.isMap = exports.isRegExp = exports.isBoolean = exports.isNumber = exports.isString = exports.isArray = exports.isEmptyObject = exports.isPlainObject = exports.isNull = exports.isUndefined = void 0; | ||
var getType = function (payload) { | ||
@@ -87,2 +87,6 @@ return Object.prototype.toString.call(payload).slice(8, -1); | ||
exports.isInfinite = isInfinite; | ||
var isTypedArray = function (payload) { | ||
return ArrayBuffer.isView(payload) && !(payload instanceof DataView); | ||
}; | ||
exports.isTypedArray = isTypedArray; | ||
//# sourceMappingURL=is.js.map |
export declare type PrimitiveTypeAnnotation = 'number' | 'undefined' | 'bigint'; | ||
declare type LeafTypeAnnotation = PrimitiveTypeAnnotation | 'regexp' | 'Date' | 'Error'; | ||
declare type TypedArrayAnnotation = ['typed-array', string]; | ||
declare type ClassTypeAnnotation = ['class', string]; | ||
@@ -7,3 +8,3 @@ declare type SymbolTypeAnnotation = ['symbol', string]; | ||
declare type SimpleTypeAnnotation = LeafTypeAnnotation | 'map' | 'set'; | ||
declare type CompositeTypeAnnotation = ClassTypeAnnotation | SymbolTypeAnnotation | CustomTypeAnnotation; | ||
declare type CompositeTypeAnnotation = TypedArrayAnnotation | ClassTypeAnnotation | SymbolTypeAnnotation | CustomTypeAnnotation; | ||
export declare type TypeAnnotation = SimpleTypeAnnotation | CompositeTypeAnnotation; | ||
@@ -10,0 +11,0 @@ export declare function isInstanceOfRegisteredClass(potentialClass: any): potentialClass is any; |
@@ -127,2 +127,23 @@ "use strict"; | ||
}); | ||
var constructorToName = [ | ||
Int8Array, | ||
Uint8Array, | ||
Int16Array, | ||
Uint16Array, | ||
Int32Array, | ||
Uint32Array, | ||
Float32Array, | ||
Float64Array, | ||
Uint8ClampedArray, | ||
].reduce(function (obj, ctor) { | ||
obj[ctor.name] = ctor; | ||
return obj; | ||
}, {}); | ||
var typedArrayRule = compositeTransformation(is_1.isTypedArray, function (v) { return ['typed-array', v.constructor.name]; }, function (v) { return __spreadArray([], __read(v)); }, function (v, a) { | ||
var ctor = constructorToName[a[1]]; | ||
if (!ctor) { | ||
throw new Error('Trying to deserialize unknown typed array'); | ||
} | ||
return new ctor(v); | ||
}); | ||
function isInstanceOfRegisteredClass(potentialClass) { | ||
@@ -171,3 +192,3 @@ if (potentialClass === null || potentialClass === void 0 ? void 0 : potentialClass.constructor) { | ||
}); | ||
var compositeRules = [classRule, symbolRule, customRule]; | ||
var compositeRules = [classRule, symbolRule, customRule, typedArrayRule]; | ||
var transformValue = function (value) { | ||
@@ -208,2 +229,4 @@ var applicableCompositeRule = util_1.findArr(compositeRules, function (rule) { | ||
return customRule.untransform(json, type); | ||
case 'typed-array': | ||
return typedArrayRule.untransform(json, type); | ||
default: | ||
@@ -210,0 +233,0 @@ throw new Error('Unknown transformation: ' + type); |
{ | ||
"version": "1.7.5", | ||
"version": "1.8.0", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -6,3 +6,10 @@ /* eslint-disable es5/no-for-of */ | ||
import { JSONValue, SuperJSONResult, SuperJSONValue } from './types'; | ||
import { isArray, isMap, isPlainObject, isPrimitive, isSet } from './is'; | ||
import { | ||
isArray, | ||
isMap, | ||
isPlainObject, | ||
isPrimitive, | ||
isSet, | ||
isTypedArray, | ||
} from './is'; | ||
@@ -542,2 +549,19 @@ import { ObjectID } from 'mongodb'; | ||
'works with typed arrays': { | ||
input: { | ||
a: new Int8Array([1, 2]), | ||
b: new Uint8ClampedArray(3), | ||
}, | ||
output: { | ||
a: [1, 2], | ||
b: [0, 0, 0], | ||
}, | ||
outputAnnotations: { | ||
values: { | ||
a: [['typed-array', 'Int8Array']], | ||
b: [['typed-array', 'Uint8ClampedArray']], | ||
}, | ||
}, | ||
}, | ||
'works for undefined, issue #48': { | ||
@@ -595,2 +619,6 @@ input: undefined, | ||
if (isTypedArray(object)) { | ||
return; | ||
} | ||
if (alreadySeenObjects.has(object)) { | ||
@@ -597,0 +625,0 @@ return; |
@@ -13,2 +13,3 @@ import { | ||
isPlainObject, | ||
isTypedArray, | ||
} from './is'; | ||
@@ -33,2 +34,3 @@ | ||
expect(isSymbol(Symbol())).toBe(true); | ||
expect(isTypedArray(new Uint8Array())).toBe(true); | ||
}); | ||
@@ -49,2 +51,4 @@ | ||
expect(isSymbol(NaN)).toBe(false); | ||
expect(isTypedArray([])).toBe(false); | ||
}); | ||
@@ -51,0 +55,0 @@ |
@@ -71,1 +71,17 @@ const getType = (payload: any): string => | ||
payload === Infinity || payload === -Infinity; | ||
export type TypedArrayConstructor = | ||
| Int8ArrayConstructor | ||
| Uint8ArrayConstructor | ||
| Uint8ClampedArrayConstructor | ||
| Int16ArrayConstructor | ||
| Uint16ArrayConstructor | ||
| Int32ArrayConstructor | ||
| Uint32ArrayConstructor | ||
| Float32ArrayConstructor | ||
| Float64ArrayConstructor; | ||
export type TypedArray = InstanceType<TypedArrayConstructor>; | ||
export const isTypedArray = (payload: any): payload is TypedArray => | ||
ArrayBuffer.isView(payload) && !(payload instanceof DataView); |
@@ -13,2 +13,4 @@ import { | ||
isError, | ||
isTypedArray, | ||
TypedArrayConstructor, | ||
} from './is'; | ||
@@ -25,2 +27,3 @@ import { ClassRegistry } from './class-registry'; | ||
type TypedArrayAnnotation = ['typed-array', string]; | ||
type ClassTypeAnnotation = ['class', string]; | ||
@@ -33,2 +36,3 @@ type SymbolTypeAnnotation = ['symbol', string]; | ||
type CompositeTypeAnnotation = | ||
| TypedArrayAnnotation | ||
| ClassTypeAnnotation | ||
@@ -199,2 +203,32 @@ | SymbolTypeAnnotation | ||
const constructorToName = [ | ||
Int8Array, | ||
Uint8Array, | ||
Int16Array, | ||
Uint16Array, | ||
Int32Array, | ||
Uint32Array, | ||
Float32Array, | ||
Float64Array, | ||
Uint8ClampedArray, | ||
].reduce<Record<string, TypedArrayConstructor>>((obj, ctor) => { | ||
obj[ctor.name] = ctor; | ||
return obj; | ||
}, {}); | ||
const typedArrayRule = compositeTransformation( | ||
isTypedArray, | ||
v => ['typed-array', v.constructor.name], | ||
v => [...v], | ||
(v, a) => { | ||
const ctor = constructorToName[a[1]]; | ||
if (!ctor) { | ||
throw new Error('Trying to deserialize unknown typed array'); | ||
} | ||
return new ctor(v); | ||
} | ||
); | ||
export function isInstanceOfRegisteredClass( | ||
@@ -264,3 +298,3 @@ potentialClass: any | ||
const compositeRules = [classRule, symbolRule, customRule]; | ||
const compositeRules = [classRule, symbolRule, customRule, typedArrayRule]; | ||
@@ -308,2 +342,4 @@ export const transformValue = ( | ||
return customRule.untransform(json, type); | ||
case 'typed-array': | ||
return typedArrayRule.untransform(json, type); | ||
default: | ||
@@ -310,0 +346,0 @@ throw new Error('Unknown transformation: ' + type); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
203907
4119