CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
utils.js321 linesDownload Raw Back to lib
1'use strict';2 3var formats = require('./formats');4var getSideChannel = require('side-channel');5 6var has = Object.prototype.hasOwnProperty;7var isArray = Array.isArray;8 9// Track objects created from arrayLimit overflow using side-channel10// Stores the current max numeric index for O(1) lookup11var overflowChannel = getSideChannel();12 13var markOverflow = function markOverflow(obj, maxIndex) {14    overflowChannel.set(obj, maxIndex);15    return obj;16};17 18var isOverflow = function isOverflow(obj) {19    return overflowChannel.has(obj);20};21 22var getMaxIndex = function getMaxIndex(obj) {23    return overflowChannel.get(obj);24};25 26var setMaxIndex = function setMaxIndex(obj, maxIndex) {27    overflowChannel.set(obj, maxIndex);28};29 30var hexTable = (function () {31    var array = [];32    for (var i = 0; i < 256; ++i) {33        array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());34    }35 36    return array;37}());38 39var compactQueue = function compactQueue(queue) {40    while (queue.length > 1) {41        var item = queue.pop();42        var obj = item.obj[item.prop];43 44        if (isArray(obj)) {45            var compacted = [];46 47            for (var j = 0; j < obj.length; ++j) {48                if (typeof obj[j] !== 'undefined') {49                    compacted.push(obj[j]);50                }51            }52 53            item.obj[item.prop] = compacted;54        }55    }56};57 58var arrayToObject = function arrayToObject(source, options) {59    var obj = options && options.plainObjects ? { __proto__: null } : {};60    for (var i = 0; i < source.length; ++i) {61        if (typeof source[i] !== 'undefined') {62            obj[i] = source[i];63        }64    }65 66    return obj;67};68 69var merge = function merge(target, source, options) {70    /* eslint no-param-reassign: 0 */71    if (!source) {72        return target;73    }74 75    if (typeof source !== 'object' && typeof source !== 'function') {76        if (isArray(target)) {77            target.push(source);78        } else if (target && typeof target === 'object') {79            if (isOverflow(target)) {80                // Add at next numeric index for overflow objects81                var newIndex = getMaxIndex(target) + 1;82                target[newIndex] = source;83                setMaxIndex(target, newIndex);84            } else if (85                (options && (options.plainObjects || options.allowPrototypes))86                || !has.call(Object.prototype, source)87            ) {88                target[source] = true;89            }90        } else {91            return [target, source];92        }93 94        return target;95    }96 97    if (!target || typeof target !== 'object') {98        if (isOverflow(source)) {99            // Create new object with target at 0, source values shifted by 1100            var sourceKeys = Object.keys(source);101            var result = options && options.plainObjects102                ? { __proto__: null, 0: target }103                : { 0: target };104            for (var m = 0; m < sourceKeys.length; m++) {105                var oldKey = parseInt(sourceKeys[m], 10);106                result[oldKey + 1] = source[sourceKeys[m]];107            }108            return markOverflow(result, getMaxIndex(source) + 1);109        }110        return [target].concat(source);111    }112 113    var mergeTarget = target;114    if (isArray(target) && !isArray(source)) {115        mergeTarget = arrayToObject(target, options);116    }117 118    if (isArray(target) && isArray(source)) {119        source.forEach(function (item, i) {120            if (has.call(target, i)) {121                var targetItem = target[i];122                if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {123                    target[i] = merge(targetItem, item, options);124                } else {125                    target.push(item);126                }127            } else {128                target[i] = item;129            }130        });131        return target;132    }133 134    return Object.keys(source).reduce(function (acc, key) {135        var value = source[key];136 137        if (has.call(acc, key)) {138            acc[key] = merge(acc[key], value, options);139        } else {140            acc[key] = value;141        }142        return acc;143    }, mergeTarget);144};145 146var assign = function assignSingleSource(target, source) {147    return Object.keys(source).reduce(function (acc, key) {148        acc[key] = source[key];149        return acc;150    }, target);151};152 153var decode = function (str, defaultDecoder, charset) {154    var strWithoutPlus = str.replace(/\+/g, ' ');155    if (charset === 'iso-8859-1') {156        // unescape never throws, no try...catch needed:157        return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);158    }159    // utf-8160    try {161        return decodeURIComponent(strWithoutPlus);162    } catch (e) {163        return strWithoutPlus;164    }165};166 167var limit = 1024;168 169/* eslint operator-linebreak: [2, "before"] */170 171var encode = function encode(str, defaultEncoder, charset, kind, format) {172    // This code was originally written by Brian White (mscdex) for the io.js core querystring library.173    // It has been adapted here for stricter adherence to RFC 3986174    if (str.length === 0) {175        return str;176    }177 178    var string = str;179    if (typeof str === 'symbol') {180        string = Symbol.prototype.toString.call(str);181    } else if (typeof str !== 'string') {182        string = String(str);183    }184 185    if (charset === 'iso-8859-1') {186        return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {187            return '%26%23' + parseInt($0.slice(2), 16) + '%3B';188        });189    }190 191    var out = '';192    for (var j = 0; j < string.length; j += limit) {193        var segment = string.length >= limit ? string.slice(j, j + limit) : string;194        var arr = [];195 196        for (var i = 0; i < segment.length; ++i) {197            var c = segment.charCodeAt(i);198            if (199                c === 0x2D // -200                || c === 0x2E // .201                || c === 0x5F // _202                || c === 0x7E // ~203                || (c >= 0x30 && c <= 0x39) // 0-9204                || (c >= 0x41 && c <= 0x5A) // a-z205                || (c >= 0x61 && c <= 0x7A) // A-Z206                || (format === formats.RFC1738 && (c === 0x28 || c === 0x29)) // ( )207            ) {208                arr[arr.length] = segment.charAt(i);209                continue;210            }211 212            if (c < 0x80) {213                arr[arr.length] = hexTable[c];214                continue;215            }216 217            if (c < 0x800) {218                arr[arr.length] = hexTable[0xC0 | (c >> 6)]219                    + hexTable[0x80 | (c & 0x3F)];220                continue;221            }222 223            if (c < 0xD800 || c >= 0xE000) {224                arr[arr.length] = hexTable[0xE0 | (c >> 12)]225                    + hexTable[0x80 | ((c >> 6) & 0x3F)]226                    + hexTable[0x80 | (c & 0x3F)];227                continue;228            }229 230            i += 1;231            c = 0x10000 + (((c & 0x3FF) << 10) | (segment.charCodeAt(i) & 0x3FF));232 233            arr[arr.length] = hexTable[0xF0 | (c >> 18)]234                + hexTable[0x80 | ((c >> 12) & 0x3F)]235                + hexTable[0x80 | ((c >> 6) & 0x3F)]236                + hexTable[0x80 | (c & 0x3F)];237        }238 239        out += arr.join('');240    }241 242    return out;243};244 245var compact = function compact(value) {246    var queue = [{ obj: { o: value }, prop: 'o' }];247    var refs = [];248 249    for (var i = 0; i < queue.length; ++i) {250        var item = queue[i];251        var obj = item.obj[item.prop];252 253        var keys = Object.keys(obj);254        for (var j = 0; j < keys.length; ++j) {255            var key = keys[j];256            var val = obj[key];257            if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {258                queue.push({ obj: obj, prop: key });259                refs.push(val);260            }261        }262    }263 264    compactQueue(queue);265 266    return value;267};268 269var isRegExp = function isRegExp(obj) {270    return Object.prototype.toString.call(obj) === '[object RegExp]';271};272 273var isBuffer = function isBuffer(obj) {274    if (!obj || typeof obj !== 'object') {275        return false;276    }277 278    return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));279};280 281var combine = function combine(a, b, arrayLimit, plainObjects) {282    // If 'a' is already an overflow object, add to it283    if (isOverflow(a)) {284        var newIndex = getMaxIndex(a) + 1;285        a[newIndex] = b;286        setMaxIndex(a, newIndex);287        return a;288    }289 290    var result = [].concat(a, b);291    if (result.length > arrayLimit) {292        return markOverflow(arrayToObject(result, { plainObjects: plainObjects }), result.length - 1);293    }294    return result;295};296 297var maybeMap = function maybeMap(val, fn) {298    if (isArray(val)) {299        var mapped = [];300        for (var i = 0; i < val.length; i += 1) {301            mapped.push(fn(val[i]));302        }303        return mapped;304    }305    return fn(val);306};307 308module.exports = {309    arrayToObject: arrayToObject,310    assign: assign,311    combine: combine,312    compact: compact,313    decode: decode,314    encode: encode,315    isBuffer: isBuffer,316    isOverflow: isOverflow,317    isRegExp: isRegExp,318    maybeMap: maybeMap,319    merge: merge320};321