-
Notifications
You must be signed in to change notification settings - Fork 6
/
tpyo.js
60 lines (50 loc) · 1.58 KB
/
tpyo.js
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
/*! https://mths.be/tpyo v2.0.0 by @mathias | MIT license */
(function() {
'use strict';
const Levenshtein = require('levenshtein');
// inspired by @alvasnaedis’ http://jsfiddle.net/vGt8v/
const getProperties = function(object, map) {
// TODO: should we support `__proto__`?
map || (map = {});
for (const property of Object.getOwnPropertyNames(object)) {
// Any truthy value would work here, but when `property` is `__proto__`,
// the value must be either `null` (which is falsy) or an object. Hence,
// `new Boolean(true)`.
map[property] = new Boolean(true);
}
const proto = object.__proto__;
if (proto) {
getProperties(proto, map);
}
return Object.keys(map);
};
const findSimilarProperty = function(property, properties) {
let lowestDistance = Infinity;
return properties.reduce(function(previousValue, currentValue) {
const distance = (new Levenshtein(currentValue, property)).distance;
if (distance < lowestDistance) {
lowestDistance = distance;
return currentValue;
}
return previousValue;
}, '');
};
const getProperty = function(target, name, receiver) {
if (name in target) {
return target[name];
}
// Get all properties.
const properties = getProperties(target);
// Find the most similar-looking property name.
const closestProperty = findSimilarProperty(name, properties);
//console.log('%s \u2192 %s', name, closestProperty);
return target[closestProperty];
};
const tpyo = function(something) {
return new Proxy(something, {
'get': getProperty
});
};
tpyo.version = '2.0.0';
module.exports = tpyo;
}());