Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const l = require('fast-levenshtein');
- const has = (o, p) => Object.prototype.hasOwnProperty.call(o, p);
- function leven(target, list) {
- const sorted = [];
- for (const item of list) {
- const score = l.get(target, item);
- sorted.splice(+score - 1, 0, item);
- }
- let ret;
- while ((ret = sorted.shift()) == null) {}
- return ret;
- }
- function find(target, prop, value, setting) {
- prop = has(target, prop) ? prop : leven(prop, Object.keys(target));
- if (setting) return target[prop] = value;
- return target[prop];
- }
- const handler = {
- get(target, prop) {
- const found = find(target, prop);
- return typeof found === 'object' ? new Proxy(found, handler) : found;
- },
- set(target, prop, value) {
- const found = find(target, prop, value, true);
- return typeof found === 'object' ? new Proxy(found, handler) : found;
- },
- };
- function autocorrect(obj) {
- return new Proxy(obj, handler);
- };
- if (typeof module !== 'undefined') module.exports = autocorrect;
- if (typeof window !== 'undefined') window.objAutocorrect = autocorrect;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement