CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
utils.js382 linesDownload Raw Back to test
1'use strict';2 3var test = require('tape');4var inspect = require('object-inspect');5var SaferBuffer = require('safer-buffer').Buffer;6var forEach = require('for-each');7var v = require('es-value-fixtures');8 9var utils = require('../lib/utils');10 11test('merge()', function (t) {12    t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null');13 14    t.deepEqual(utils.merge(null, [42]), [null, 42], 'merges null into an array');15 16    t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key');17 18    var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });19    t.deepEqual(oneMerged, { foo: ['bar', { first: '123' }] }, 'merges a standalone and an object into an array');20 21    var twoMerged = utils.merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } });22    t.deepEqual(twoMerged, { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, 'merges a standalone and two objects into an array');23 24    var sandwiched = utils.merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' });25    t.deepEqual(sandwiched, { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, 'merges an object sandwiched by two standalones into an array');26 27    var nestedArrays = utils.merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] });28    t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] });29 30    var noOptionsNonObjectSource = utils.merge({ foo: 'baz' }, 'bar');31    t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });32 33    var func = function f() {};34    t.deepEqual(35        utils.merge(func, { foo: 'bar' }),36        [func, { foo: 'bar' }],37        'functions can not be merged into'38    );39 40    func.bar = 'baz';41    t.deepEqual(42        utils.merge({ foo: 'bar' }, func),43        { foo: 'bar', bar: 'baz' },44        'functions can be merge sources'45    );46 47    t.test(48        'avoids invoking array setters unnecessarily',49        { skip: typeof Object.defineProperty !== 'function' },50        function (st) {51            var setCount = 0;52            var getCount = 0;53            var observed = [];54            Object.defineProperty(observed, 0, {55                get: function () {56                    getCount += 1;57                    return { bar: 'baz' };58                },59                set: function () { setCount += 1; }60            });61            utils.merge(observed, [null]);62            st.equal(setCount, 0);63            st.equal(getCount, 1);64            observed[0] = observed[0]; // eslint-disable-line no-self-assign65            st.equal(setCount, 1);66            st.equal(getCount, 2);67            st.end();68        }69    );70 71    t.test('with overflow objects (from arrayLimit)', function (st) {72        st.test('merges primitive into overflow object at next index', function (s2t) {73            // Create an overflow object via combine74            var overflow = utils.combine(['a'], 'b', 1, false);75            s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');76            var merged = utils.merge(overflow, 'c');77            s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c' }, 'adds primitive at next numeric index');78            s2t.end();79        });80 81        st.test('merges primitive into regular object with numeric keys normally', function (s2t) {82            var obj = { 0: 'a', 1: 'b' };83            s2t.notOk(utils.isOverflow(obj), 'plain object is not marked as overflow');84            var merged = utils.merge(obj, 'c');85            s2t.deepEqual(merged, { 0: 'a', 1: 'b', c: true }, 'adds primitive as key (not at next index)');86            s2t.end();87        });88 89        st.test('merges primitive into object with non-numeric keys normally', function (s2t) {90            var obj = { foo: 'bar' };91            var merged = utils.merge(obj, 'baz');92            s2t.deepEqual(merged, { foo: 'bar', baz: true }, 'adds primitive as key with value true');93            s2t.end();94        });95 96        st.test('merges overflow object into primitive', function (s2t) {97            // Create an overflow object via combine98            var overflow = utils.combine([], 'b', 0, false);99            s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');100            var merged = utils.merge('a', overflow);101            s2t.ok(utils.isOverflow(merged), 'result is also marked as overflow');102            s2t.deepEqual(merged, { 0: 'a', 1: 'b' }, 'creates object with primitive at 0, source values shifted');103            s2t.end();104        });105 106        st.test('merges overflow object with multiple values into primitive', function (s2t) {107            // Create an overflow object via combine108            var overflow = utils.combine(['b'], 'c', 1, false);109            s2t.ok(utils.isOverflow(overflow), 'overflow object is marked');110            var merged = utils.merge('a', overflow);111            s2t.deepEqual(merged, { 0: 'a', 1: 'b', 2: 'c' }, 'shifts all source indices by 1');112            s2t.end();113        });114 115        st.test('merges regular object into primitive as array', function (s2t) {116            var obj = { foo: 'bar' };117            var merged = utils.merge('a', obj);118            s2t.deepEqual(merged, ['a', { foo: 'bar' }], 'creates array with primitive and object');119            s2t.end();120        });121 122        st.end();123    });124 125    t.end();126});127 128test('assign()', function (t) {129    var target = { a: 1, b: 2 };130    var source = { b: 3, c: 4 };131    var result = utils.assign(target, source);132 133    t.equal(result, target, 'returns the target');134    t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');135    t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');136 137    t.end();138});139 140test('combine()', function (t) {141    t.test('both arrays', function (st) {142        var a = [1];143        var b = [2];144        var combined = utils.combine(a, b);145 146        st.deepEqual(a, [1], 'a is not mutated');147        st.deepEqual(b, [2], 'b is not mutated');148        st.notEqual(a, combined, 'a !== combined');149        st.notEqual(b, combined, 'b !== combined');150        st.deepEqual(combined, [1, 2], 'combined is a + b');151 152        st.end();153    });154 155    t.test('one array, one non-array', function (st) {156        var aN = 1;157        var a = [aN];158        var bN = 2;159        var b = [bN];160 161        var combinedAnB = utils.combine(aN, b);162        st.deepEqual(b, [bN], 'b is not mutated');163        st.notEqual(aN, combinedAnB, 'aN + b !== aN');164        st.notEqual(a, combinedAnB, 'aN + b !== a');165        st.notEqual(bN, combinedAnB, 'aN + b !== bN');166        st.notEqual(b, combinedAnB, 'aN + b !== b');167        st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');168 169        var combinedABn = utils.combine(a, bN);170        st.deepEqual(a, [aN], 'a is not mutated');171        st.notEqual(aN, combinedABn, 'a + bN !== aN');172        st.notEqual(a, combinedABn, 'a + bN !== a');173        st.notEqual(bN, combinedABn, 'a + bN !== bN');174        st.notEqual(b, combinedABn, 'a + bN !== b');175        st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');176 177        st.end();178    });179 180    t.test('neither is an array', function (st) {181        var combined = utils.combine(1, 2);182        st.notEqual(1, combined, '1 + 2 !== 1');183        st.notEqual(2, combined, '1 + 2 !== 2');184        st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');185 186        st.end();187    });188 189    t.test('with arrayLimit', function (st) {190        st.test('under the limit', function (s2t) {191            var combined = utils.combine(['a', 'b'], 'c', 10, false);192            s2t.deepEqual(combined, ['a', 'b', 'c'], 'returns array when under limit');193            s2t.ok(Array.isArray(combined), 'result is an array');194            s2t.end();195        });196 197        st.test('exactly at the limit stays as array', function (s2t) {198            var combined = utils.combine(['a', 'b'], 'c', 3, false);199            s2t.deepEqual(combined, ['a', 'b', 'c'], 'stays as array when exactly at limit');200            s2t.ok(Array.isArray(combined), 'result is an array');201            s2t.end();202        });203 204        st.test('over the limit', function (s2t) {205            var combined = utils.combine(['a', 'b', 'c'], 'd', 3, false);206            s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c', 3: 'd' }, 'converts to object when over limit');207            s2t.notOk(Array.isArray(combined), 'result is not an array');208            s2t.end();209        });210 211        st.test('with arrayLimit 0', function (s2t) {212            var combined = utils.combine([], 'a', 0, false);213            s2t.deepEqual(combined, { 0: 'a' }, 'converts single element to object with arrayLimit 0');214            s2t.notOk(Array.isArray(combined), 'result is not an array');215            s2t.end();216        });217 218        st.test('with plainObjects option', function (s2t) {219            var combined = utils.combine(['a'], 'b', 1, true);220            var expected = { __proto__: null, 0: 'a', 1: 'b' };221            s2t.deepEqual(combined, expected, 'converts to object with null prototype');222            s2t.equal(Object.getPrototypeOf(combined), null, 'result has null prototype when plainObjects is true');223            s2t.end();224        });225 226        st.end();227    });228 229    t.test('with existing overflow object', function (st) {230        st.test('adds to existing overflow object at next index', function (s2t) {231            // Create overflow object first via combine232            var overflow = utils.combine(['a'], 'b', 1, false);233            s2t.ok(utils.isOverflow(overflow), 'initial object is marked as overflow');234 235            var combined = utils.combine(overflow, 'c', 10, false);236            s2t.equal(combined, overflow, 'returns the same object (mutated)');237            s2t.deepEqual(combined, { 0: 'a', 1: 'b', 2: 'c' }, 'adds value at next numeric index');238            s2t.end();239        });240 241        st.test('does not treat plain object with numeric keys as overflow', function (s2t) {242            var plainObj = { 0: 'a', 1: 'b' };243            s2t.notOk(utils.isOverflow(plainObj), 'plain object is not marked as overflow');244 245            // combine treats this as a regular value, not an overflow object to append to246            var combined = utils.combine(plainObj, 'c', 10, false);247            s2t.deepEqual(combined, [{ 0: 'a', 1: 'b' }, 'c'], 'concatenates as regular values');248            s2t.end();249        });250 251        st.end();252    });253 254    t.end();255});256 257test('decode', function (t) {258    t.equal(259        utils.decode('a+b'),260        'a b',261        'decodes + to space'262    );263 264    t.equal(265        utils.decode('name%2Eobj'),266        'name.obj',267        'decodes a string'268    );269    t.equal(270        utils.decode('name%2Eobj%2Efoo', null, 'iso-8859-1'),271        'name.obj.foo',272        'decodes a string in iso-8859-1'273    );274 275    t.end();276});277 278test('encode', function (t) {279    forEach(v.nullPrimitives, function (nullish) {280        t['throws'](281            function () { utils.encode(nullish); },282            TypeError,283            inspect(nullish) + ' is not a string'284        );285    });286 287    t.equal(utils.encode(''), '', 'empty string returns itself');288    t.deepEqual(utils.encode([]), [], 'empty array returns itself');289    t.deepEqual(utils.encode({ length: 0 }), { length: 0 }, 'empty arraylike returns itself');290 291    t.test('symbols', { skip: !v.hasSymbols }, function (st) {292        st.equal(utils.encode(Symbol('x')), 'Symbol%28x%29', 'symbol is encoded');293 294        st.end();295    });296 297    t.equal(298        utils.encode('(abc)'),299        '%28abc%29',300        'encodes parentheses'301    );302    t.equal(303        utils.encode({ toString: function () { return '(abc)'; } }),304        '%28abc%29',305        'toStrings and encodes parentheses'306    );307 308    t.equal(309        utils.encode('abc 123 ๐Ÿ’ฉ', null, 'iso-8859-1'),310        'abc%20123%20%26%2355357%3B%26%2356489%3B',311        'encodes in iso-8859-1'312    );313 314    var longString = '';315    var expectedString = '';316    for (var i = 0; i < 1500; i++) {317        longString += ' ';318        expectedString += '%20';319    }320 321    t.equal(322        utils.encode(longString),323        expectedString,324        'encodes a long string'325    );326 327    t.equal(328        utils.encode('\x28\x29'),329        '%28%29',330        'encodes parens normally'331    );332    t.equal(333        utils.encode('\x28\x29', null, null, null, 'RFC1738'),334        '()',335        'does not encode parens in RFC1738'336    );337 338    // todo RFC1738 format339 340    t.equal(341        utils.encode('ฤ€แ€€๏ค€'),342        '%C4%80%E1%80%80%EF%A4%80',343        'encodes multibyte chars'344    );345 346    t.equal(347        utils.encode('\uD83D \uDCA9'),348        '%F0%9F%90%A0%F0%BA%90%80',349        'encodes lone surrogates'350    );351 352    t.end();353});354 355test('isBuffer()', function (t) {356    forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) {357        t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');358    });359 360    var fakeBuffer = { constructor: Buffer };361    t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');362 363    var saferBuffer = SaferBuffer.from('abc');364    t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');365 366    var buffer = Buffer.from && Buffer.alloc ? Buffer.from('abc') : new Buffer('abc');367    t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer');368    t.end();369});370 371test('isRegExp()', function (t) {372    t.equal(utils.isRegExp(/a/g), true, 'RegExp is a RegExp');373    t.equal(utils.isRegExp(new RegExp('a', 'g')), true, 'new RegExp is a RegExp');374    t.equal(utils.isRegExp(new Date()), false, 'Date is not a RegExp');375 376    forEach(v.primitives, function (primitive) {377        t.equal(utils.isRegExp(primitive), false, inspect(primitive) + ' is not a RegExp');378    });379 380    t.end();381});382