Skip to content

Commit 1f461d8

Browse files
author
Emidio Croci
committed
Fixed issue converting ArrayBuffer to empty object
1 parent 0a0445d commit 1f461d8

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/deep-map-keys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import WeakMap = require('es6-weak-map');
2-
import {isArray, isObject} from 'lodash';
2+
import {isArray, isObject, isArrayBuffer} from 'lodash';
33

44
interface NonPrimitive extends Object {
55
[key: string]: any;
@@ -25,7 +25,7 @@ export class DeepMapKeys {
2525

2626
public map(value: any): any {
2727
return isArray(value) ? this.mapArray(value) :
28-
isObject(value) ? this.mapObject(value) :
28+
isArrayBuffer(value) ? value : isObject(value) ? this.mapObject(value) :
2929
value;
3030
}
3131

src/index.test.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,43 @@ describe('deepMapKeys(object, mapFn, [options])', () => {
2222
describe('@object: any', () => {
2323

2424
it('transforms keys of simple object', () => {
25-
deepMapKeys({one: 1, two: 2}, caps).should.deep.equal({ONE: 1, TWO: 2});
25+
deepMapKeys({ one: 1, two: 2 }, caps).should.deep.equal({ ONE: 1, TWO: 2 });
2626
});
2727

2828
it('transforms keys of object with nested objects/arrays', () => {
29-
deepMapKeys({one: 1, obj: {two: 2, three: 3}, arr: [4, 5]}, caps)
30-
.should.deep.equal({ONE: 1, OBJ: {TWO: 2, THREE: 3}, ARR: [4, 5]});
29+
deepMapKeys({ one: 1, obj: { two: 2, three: 3 }, arr: [4, 5] }, caps)
30+
.should.deep.equal({ ONE: 1, OBJ: { TWO: 2, THREE: 3 }, ARR: [4, 5] });
3131
});
3232

3333
it('transforms keys of array with nested object/array', () => {
34-
deepMapKeys([1, {two: 2, three: 3, arr: [4, {five: 5}]}], caps)
35-
.should.deep.equal([1, {TWO: 2, THREE: 3, ARR: [4, {FIVE: 5}]}]);
34+
deepMapKeys([1, { two: 2, three: 3, arr: [4, { five: 5 }] }], caps)
35+
.should.deep.equal([1, { TWO: 2, THREE: 3, ARR: [4, { FIVE: 5 }] }]);
36+
});
37+
38+
function str2ab(str: string): ArrayBuffer {
39+
let buf = new ArrayBuffer(str.length * 2);
40+
let bufView = new Uint16Array(buf);
41+
for (let i = 0, strLen = str.length; i < strLen; i++) {
42+
bufView[i] = str.charCodeAt(i);
43+
}
44+
return buf;
45+
}
46+
function ab2str(buf: ArrayBuffer): string {
47+
return String.fromCharCode.apply(null, new Uint16Array(buf));
48+
}
49+
50+
it('doesn\'t transform an ArrayBuffer to an empty array', () => {
51+
const buffer = str2ab('test');
52+
const transformed: any = deepMapKeys({ two: 2, three: 3, arr: buffer }, caps);
53+
ab2str(transformed.ARR).should.be.equal('test');
3654
});
3755

3856
it('transforms an object with circular references', () => {
39-
let obj = {one: 1, arr: [2, 3], self: null as any, arr2: null as any[]};
57+
let obj = { one: 1, arr: [2, 3], self: null as any, arr2: null as any[] };
4058
obj.self = obj;
4159
obj.arr2 = obj.arr;
4260

43-
let exp = {ONE: 1, ARR: [2, 3], SELF: null as any, ARR2: null as any[]};
61+
let exp = { ONE: 1, ARR: [2, 3], SELF: null as any, ARR2: null as any[] };
4462
exp.SELF = exp;
4563
exp.ARR2 = exp.ARR;
4664

@@ -52,27 +70,27 @@ describe('deepMapKeys(object, mapFn, [options])', () => {
5270
describe('@mapFn(key: string, value: any): string', () => {
5371

5472
it('throws Error if undefined', () => {
55-
deepMapKeys.bind(null, {one: 1}).should.throw(Error);
73+
deepMapKeys.bind(null, { one: 1 }).should.throw(Error);
5674
});
5775

5876
it('throws TypeError if not a function', () => {
59-
deepMapKeys.bind(null, {one: 1}, 42).should.throw(TypeError);
77+
deepMapKeys.bind(null, { one: 1 }, 42).should.throw(TypeError);
6078
});
6179

6280
it('is called once per object property', () => {
63-
deepMapKeys({one: 1, obj: {two: 2, three: 3}, arr: [4, 5]}, caps);
81+
deepMapKeys({ one: 1, obj: { two: 2, three: 3 }, arr: [4, 5] }, caps);
6482
caps.should.have.callCount(5);
6583
});
6684

6785
it('is called with @key as first argument', () => {
68-
deepMapKeys({one: 1, arr: [2, 3]}, caps);
86+
deepMapKeys({ one: 1, arr: [2, 3] }, caps);
6987
caps.should.have.been.calledWith('one');
7088
caps.should.have.been.calledWith('arr');
7189
});
7290

7391
it('is called with @value as second argument', () => {
74-
let {any} = sinon.match;
75-
deepMapKeys({one: 1, arr: [2, 3]}, caps);
92+
let { any } = sinon.match;
93+
deepMapKeys({ one: 1, arr: [2, 3] }, caps);
7694
caps.should.have.been.calledWith(any, 1);
7795
caps.should.have.been.calledWithMatch(any, [2, 3]);
7896
});
@@ -82,18 +100,18 @@ describe('deepMapKeys(object, mapFn, [options])', () => {
82100
describe('@options?', () => {
83101

84102
it('throws TypeError if defined but not an object', () => {
85-
deepMapKeys.bind(null, {one: 1}, caps, 42).should.throw(TypeError);
103+
deepMapKeys.bind(null, { one: 1 }, caps, 42).should.throw(TypeError);
86104
});
87105

88106
describe('option: thisArg', () => {
89107

90108
it('sets context within @mapFn', () => {
91-
deepMapKeys({one: 1, arr: [2, 3]}, caps, {thisArg: 42});
109+
deepMapKeys({ one: 1, arr: [2, 3] }, caps, { thisArg: 42 });
92110
caps.should.have.been.calledOn(42);
93111
});
94112

95113
it('defaults to undefined', () => {
96-
deepMapKeys({one: 1, arr: [2, 3]}, caps);
114+
deepMapKeys({ one: 1, arr: [2, 3] }, caps);
97115
caps.should.have.been.calledOn(undefined);
98116
});
99117

0 commit comments

Comments
 (0)