This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtransform-map.js
More file actions
60 lines (55 loc) · 1.31 KB
/
transform-map.js
File metadata and controls
60 lines (55 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
@module ember-jsonapi-resources
@submodule utils
@main TransformMap
**/
import { isBlank, isType } from './is';
/**
Abstract class to transform mapped data structures
@class TransformMap
@constructor
**/
export default class TransformMap {
/**
@method constructor
@param {Object} [map] created with `null` as prototype
**/
constructor(map) {
this.map = map;
this.keys = Object.keys(map);
let inverse = Object.create(null);
let values = [];
let entries = [];
let pair;
for (let key in map) {
values.push(map[key]);
inverse[map[key]] = key;
pair = Object.create(null);
entries.push([key, map[key]]);
}
Object.freeze(inverse);
this.values = values;
this.inverse = inverse;
this.entries = entries;
}
/**
@method lookup
@param {String} [value]
@parm {String} [use='keys'] keys or values
@return {String|Null} [value] name or null
*/
lookup(value, use = 'keys') {
if (isBlank(value) || value === '') {
value = null;
} else if (isType('string', value)) {
if (this[use].indexOf(value) > -1) {
if (use === 'keys') {
value = this.map[value];
} else if (use === 'values') {
value = this.inverse[value];
}
}
}
return (value) ? value : null;
}
}