diff --git a/package-lock.json b/package-lock.json index 3939977..bea0416 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "level-out": "^1.0.1", "os-browserify": "^0.3.0", "path-browserify": "^1.0.1", + "prettier": "^2.8.8", "process": "^0.11.10", "standard": "17.1.0", "stemmer": "2.0.1", @@ -4764,6 +4765,21 @@ "node": ">=4" } }, + "node_modules/prettier": { + "version": "2.8.8", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", + "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", + "dev": true, + "bin": { + "prettier": "bin-prettier.js" + }, + "engines": { + "node": ">=10.13.0" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, "node_modules/process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", @@ -10161,6 +10177,12 @@ "integrity": "sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc=", "dev": true }, + "prettier": { + "version": "2.8.8", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.8.8.tgz", + "integrity": "sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==", + "dev": true + }, "process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", diff --git a/package.json b/package.json index f50cc10..34b2a78 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,9 @@ "level-read-stream": "1.1.0", "traverse": "0.6.7" }, + "directories": { + "lib": "src" + }, "files": [ "src" ], @@ -20,6 +23,7 @@ "level-out": "^1.0.1", "os-browserify": "^0.3.0", "path-browserify": "^1.0.1", + "prettier": "^2.8.8", "process": "^0.11.10", "standard": "17.1.0", "stemmer": "2.0.1", @@ -31,6 +35,7 @@ "webpack-cli": "^5.1.4", "world-bank-dataset": "^1.0.0" }, + "prettier": "./.prettierrc.json", "scripts": { "build": "npm run empty-sandbox && webpack", "empty-sandbox": "rm -rf test/sandbox && mkdir test/sandbox", @@ -40,6 +45,7 @@ "test-node": "npm run empty-sandbox && tape test/src/*.js", "test-script-tag-lib": "npm run build && open-cli test/src/index.html" }, + "type": "module", "repository": { "type": "git", "url": "git+https://github.com/fergiemcdowall/fergies-inverted-index" @@ -49,8 +55,5 @@ "bugs": { "url": "https://github.com/fergiemcdowall/fergies-inverted-index/issues" }, - "homepage": "https://github.com/fergiemcdowall/fergies-inverted-index", - "resolutions": { - "minimist": "^1.2.8" - } + "homepage": "https://github.com/fergiemcdowall/fergies-inverted-index" } diff --git a/src/browser.js b/src/browser.js index 7d95d7a..551c702 100644 --- a/src/browser.js +++ b/src/browser.js @@ -1,7 +1,12 @@ -const fii = require('./main.js') -const { BrowserLevel } = require('browser-level') +// import levelOptions from './options.js' +import { Main as MainInvertedIndex } from './main.js' +import { BrowserLevel } from 'browser-level' -module.exports = ({ name = 'fii', ...ops }) => fii({ - db: new BrowserLevel(name), - ...ops -}) +export class InvertedIndex { + constructor(ops = {}) { + return new MainInvertedIndex({ + db: new BrowserLevel(ops.name || 'fii', { valueEncoding: 'json' }), + ...ops + }) + } +} diff --git a/src/main.js b/src/main.js index bb0e1cb..6958fbe 100644 --- a/src/main.js +++ b/src/main.js @@ -1,7 +1,9 @@ -// const level = require('level') -const read = require('./read.js') -const write = require('./write.js') -const levelOptions = require('./options.js') +// const read = require('./read.js') +// const write = require('./write.js') +// const levelOptions = require('./options.js') + +import read from './read.js' +import write from './write.js' // _match is nested by default so that AND and OR work correctly under // the bonnet. Flatten array before presenting to consumer @@ -9,21 +11,21 @@ const flattenMatchArrayInResults = results => typeof results === 'undefined' ? undefined : results.map(result => { - // Sort _match consistently (FIELD -> VALUE -> SCORE) - result._match = result._match - .flat(Infinity) - .map(m => (typeof m === 'string' ? JSON.parse(m) : m)) - .sort((a, b) => { - if (a.FIELD < b.FIELD) return -1 - if (a.FIELD > b.FIELD) return 1 - if (a.VALUE < b.VALUE) return -1 - if (a.VALUE > b.VALUE) return 1 - if (a.SCORE < b.SCORE) return -1 - if (a.SCORE > b.SCORE) return 1 - return 0 - }) - return result - }) + // Sort _match consistently (FIELD -> VALUE -> SCORE) + result._match = result._match + .flat(Infinity) + .map(m => (typeof m === 'string' ? JSON.parse(m) : m)) + .sort((a, b) => { + if (a.FIELD < b.FIELD) return -1 + if (a.FIELD > b.FIELD) return 1 + if (a.VALUE < b.VALUE) return -1 + if (a.VALUE > b.VALUE) return 1 + if (a.SCORE < b.SCORE) return -1 + if (a.SCORE > b.SCORE) return 1 + return 0 + }) + return result + }) const initStore = (ops = {}) => new Promise((resolve, reject) => { @@ -72,7 +74,7 @@ const makeAFii = ops => { r.GET(tokens, pipeline).then(flattenMatchArrayInResults), IMPORT: w.IMPORT, LAST_UPDATED: r.LAST_UPDATED, - LEVEL_OPTIONS: levelOptions, + // LEVEL_OPTIONS: levelOptions, MAX: r.MAX, MIN: r.MIN, NOT: (...keys) => @@ -91,4 +93,8 @@ const makeAFii = ops => { })) } -module.exports = ops => initStore(ops).then(makeAFii) +export class Main { + constructor(ops) { + return initStore(ops).then(makeAFii) + } +} diff --git a/src/node.js b/src/node.js index 478abdd..82aa962 100644 --- a/src/node.js +++ b/src/node.js @@ -1,7 +1,12 @@ -const fii = require('./main.js') -const { MemoryLevel } = require('memory-level') +import levelOptions from './options.js' +import { Main } from './main.js' +import { ClassicLevel } from 'classic-level' -module.exports = ({ name = 'fii', ...ops }) => fii({ - db: new MemoryLevel(), - ...ops -}) +export class InvertedIndex { + constructor(ops = {}) { + return new Main({ + db: new ClassicLevel(ops.name || 'fii', levelOptions), + ...ops + }) + } +} diff --git a/src/options.js b/src/options.js index 6b84812..13daf16 100644 --- a/src/options.js +++ b/src/options.js @@ -1,6 +1,6 @@ -const charwise = require('charwise') +import charwise from 'charwise' -module.exports = { +export default { keyEncoding: charwise, valueEncoding: 'json' } diff --git a/src/parseToken.js b/src/parseToken.js index 0e6c30d..81257e7 100644 --- a/src/parseToken.js +++ b/src/parseToken.js @@ -5,8 +5,9 @@ charwise.HI = undefined // key might be object or string like this // :. Turn key into json object that is of the // format {FIELD: ..., VALUE: {GTE: ..., LTE ...}} -module.exports = (token, availableFields) => - new Promise((resolve, reject) => { +// module.exports = (token, availableFields) => +export default function (token, availableFields) { + return new Promise((resolve, reject) => { // case: // case: : // case: undefined @@ -106,3 +107,4 @@ module.exports = (token, availableFields) => return resolve(token) }) +} diff --git a/src/read.js b/src/read.js index 53bd277..25c0e45 100644 --- a/src/read.js +++ b/src/read.js @@ -1,13 +1,20 @@ -const tokenParser = require('./parseToken.js') -const charwise = require('charwise') -const { EntryStream } = require('level-read-stream') -const levelOptions = require('./options.js') +// const tokenParser = require('./parseToken.js') +// const charwise = require('charwise') +// const { EntryStream } = require('level-read-stream') +// const levelOptions = require('./options.js') + +import tokenParser from './parseToken.js' +import charwise from 'charwise' +import { EntryStream } from 'level-read-stream' +import levelOptions from './options.js' // polyfill- HI and LO coming in next version of charwise charwise.LO = null charwise.HI = undefined -module.exports = ops => { +// module.exports = ops => { + +export default function (ops) { const isString = s => typeof s === 'string' // TODO: in order to account for query processing pipelines, @@ -56,7 +63,7 @@ module.exports = ops => { // If this token is a stopword then return 'undefined' const removeStopwords = token => token.VALUE.GTE === token.VALUE.LTE && - ops.stopwords.includes(token.VALUE.GTE) + ops.stopwords.includes(token.VALUE.GTE) ? undefined : token @@ -84,7 +91,6 @@ module.exports = ops => { // testForBreak(token) // ? token = await queryReplace(token) // TODO: rename to replaceToken? testForBreak(token) - token = await pipeline(token) testForBreak(token) } catch (e) { @@ -201,7 +207,9 @@ module.exports = ops => { // documents exist in the index. const EXIST = (...ids) => Promise.all( - ids.map(id => ops._db.get([ops.docExistsSpace, id], levelOptions).catch(e => null)) + ids.map(id => + ops._db.get([ops.docExistsSpace, id], levelOptions).catch(e => null) + ) ).then(result => result.reduce((acc, cur, i) => { if (cur != null) acc.push(ids[i]) @@ -250,7 +258,9 @@ module.exports = ops => { const OBJECT = _ids => Promise.all( - _ids.map(id => ops._db.get(['DOC', id._id], levelOptions).catch(reason => null)) + _ids.map(id => + ops._db.get(['DOC', id._id], levelOptions).catch(reason => null) + ) ).then(_objects => _ids.map((_id, i) => { _id._object = _objects[i] diff --git a/src/write.js b/src/write.js index 950c8d5..5a278b2 100644 --- a/src/write.js +++ b/src/write.js @@ -1,8 +1,12 @@ -const trav = require('traverse') -const reader = require('./read.js') -const levelOptions = require('./options.js') +// const trav = require('traverse') +// const reader = require('./read.js') +// const levelOptions = require('./options.js') -module.exports = ops => { +import trav from 'traverse' +import reader from './read.js' +import levelOptions from './options.js' + +export default function (ops) { // TODO: set reset this to the max value every time the DB is restarted let incrementalId = 0 @@ -202,11 +206,12 @@ module.exports = ops => { // when importing, index is first cleared. This means that "merges" // are not currently supported const IMPORT = index => - ops._db - .clear() - .then(() => - ops._db.batch(index.map(entry => Object.assign(entry, { type: 'put' })), levelOptions) + ops._db.clear().then(() => + ops._db.batch( + index.map(entry => Object.assign(entry, { type: 'put' })), + levelOptions ) + ) const PUT = (docs, putOptions = {}) => writer( @@ -221,14 +226,18 @@ module.exports = ops => { ).then(TIMESTAMP_LAST_UPDATED) const TIMESTAMP_LAST_UPDATED = passThrough => - ops._db.put(['~LAST_UPDATED'], Date.now(), levelOptions).then(() => passThrough) + ops._db + .put(['~LAST_UPDATED'], Date.now(), levelOptions) + .then(() => passThrough) const TIMESTAMP_CREATED = () => ops._db .get(['~CREATED'], levelOptions) .then(/* already created- do nothing */) .catch(e => - ops._db.put(['~CREATED'], Date.now(), levelOptions).then(TIMESTAMP_LAST_UPDATED) + ops._db + .put(['~CREATED'], Date.now(), levelOptions) + .then(TIMESTAMP_LAST_UPDATED) ) return { diff --git a/test/src/AGGREGATION_FILTER-test.js b/test/src/AGGREGATION_FILTER-test.js index 6aa3fbb..ca5897d 100644 --- a/test/src/AGGREGATION_FILTER-test.js +++ b/test/src/AGGREGATION_FILTER-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'AGGREGATION_FILTER' @@ -99,7 +101,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/AND-test.js b/test/src/AND-test.js index 661f67a..a9ba369 100644 --- a/test/src/AND-test.js +++ b/test/src/AND-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'AND' @@ -99,7 +101,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/CREATED-test.js b/test/src/CREATED-test.js index c1c341a..d1549d9 100644 --- a/test/src/CREATED-test.js +++ b/test/src/CREATED-test.js @@ -1,21 +1,17 @@ -const fii = require('../../') -const levelOptions = require('../../src/options.js') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) + +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'CREATED' let timestamp -const opts = {} -if (typeof window === 'undefined') { - const { ClassicLevel } = require('classic-level') - opts.db = new ClassicLevel(indexName) -} - test('create index', t => { t.plan(1) - fii({ name: indexName, ...opts }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) @@ -23,7 +19,7 @@ test('create index', t => { test('timestamp was created', t => { t.plan(1) - global[indexName].STORE.get(['~CREATED'], levelOptions) + global[indexName].STORE.get(['~CREATED']) .then(created => { timestamp = created return t.pass('timestamp created') @@ -51,7 +47,7 @@ test('confirm index is closed', t => { test('recreate index', t => { t.plan(1) - fii({ name: indexName, ...opts }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/DELETE-test.js b/test/src/DELETE-test.js index 3fc304a..85a42e1 100644 --- a/test/src/DELETE-test.js +++ b/test/src/DELETE-test.js @@ -1,13 +1,15 @@ -const fii = require('../../') -const test = require('tape') -const wbd = require('world-bank-dataset') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' +import wbd from 'world-bank-dataset' const sandbox = 'test/sandbox/' const indexName = sandbox + 'DELETE-TEST' test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) @@ -41,81 +43,118 @@ test('can add some worldbank data', t => { test('can GET with string', t => { t.plan(1) - global[indexName].GET({ - FIELD: 'board_approval_month', - VALUE: 'November' - }) + global[indexName] + .GET({ + FIELD: 'board_approval_month', + VALUE: 'November' + }) .then(result => { t.deepEqual(result, [ - { _id: '52b213b38594d8a2be17c780', _match: [{ FIELD: 'board_approval_month', VALUE: 'November' }] }, - { _id: '52b213b38594d8a2be17c781', _match: [{ FIELD: 'board_approval_month', VALUE: 'November' }] }, - { _id: '52b213b38594d8a2be17c782', _match: [{ FIELD: 'board_approval_month', VALUE: 'November' }] } + { + _id: '52b213b38594d8a2be17c780', + _match: [{ FIELD: 'board_approval_month', VALUE: 'November' }] + }, + { + _id: '52b213b38594d8a2be17c781', + _match: [{ FIELD: 'board_approval_month', VALUE: 'November' }] + }, + { + _id: '52b213b38594d8a2be17c782', + _match: [{ FIELD: 'board_approval_month', VALUE: 'November' }] + } ]) }) }) test('can get with OBJECT', t => { t.plan(1) - global[indexName].OBJECT([ - { _id: '52b213b38594d8a2be17c781' }, - { _id: '52b213b38594d8a2be17c782' } - ]).then(result => { - t.deepEqual(result, [ - { - _id: '52b213b38594d8a2be17c781', - _object: { _id: '52b213b38594d8a2be17c781', board_approval_month: 'November', totalamt: 0 } - }, { - _id: '52b213b38594d8a2be17c782', - _object: { _id: '52b213b38594d8a2be17c782', board_approval_month: 'November', totalamt: 6060000 } - } + global[indexName] + .OBJECT([ + { _id: '52b213b38594d8a2be17c781' }, + { _id: '52b213b38594d8a2be17c782' } ]) - }) + .then(result => { + t.deepEqual(result, [ + { + _id: '52b213b38594d8a2be17c781', + _object: { + _id: '52b213b38594d8a2be17c781', + board_approval_month: 'November', + totalamt: 0 + } + }, + { + _id: '52b213b38594d8a2be17c782', + _object: { + _id: '52b213b38594d8a2be17c782', + board_approval_month: 'November', + totalamt: 6060000 + } + } + ]) + }) }) // TODO: delete keys should be in the index somewhere test('can DELETE', t => { t.plan(1) - global[indexName].DELETE([ - '52b213b38594d8a2be17c781', - 'thisIDNotFound', - '52b213b38594d8a2be17c782' - ]).then(result => { - t.deepEqual(result, [ - { _id: '52b213b38594d8a2be17c781', status: 'DELETED', operation: 'DELETE' }, - { _id: 'thisIDNotFound', status: 'FAILED', operation: 'DELETE' }, - { _id: '52b213b38594d8a2be17c782', status: 'DELETED', operation: 'DELETE' } + global[indexName] + .DELETE([ + '52b213b38594d8a2be17c781', + 'thisIDNotFound', + '52b213b38594d8a2be17c782' ]) - }) + .then(result => { + t.deepEqual(result, [ + { + _id: '52b213b38594d8a2be17c781', + status: 'DELETED', + operation: 'DELETE' + }, + { _id: 'thisIDNotFound', status: 'FAILED', operation: 'DELETE' }, + { + _id: '52b213b38594d8a2be17c782', + status: 'DELETED', + operation: 'DELETE' + } + ]) + }) }) test('can get with OBJECT', t => { t.plan(1) - global[indexName].OBJECT([ - { _id: '52b213b38594d8a2be17c780' }, - { _id: '52b213b38594d8a2be17c781' }, - { _id: '52b213b38594d8a2be17c782' } - ]).then(result => { - t.deepEqual(result, [ - - { - _id: '52b213b38594d8a2be17c780', - _object: { _id: '52b213b38594d8a2be17c780', board_approval_month: 'November', totalamt: 130000000 } - }, - { _id: '52b213b38594d8a2be17c781', _object: null }, - { _id: '52b213b38594d8a2be17c782', _object: null } + global[indexName] + .OBJECT([ + { _id: '52b213b38594d8a2be17c780' }, + { _id: '52b213b38594d8a2be17c781' }, + { _id: '52b213b38594d8a2be17c782' } ]) - }) + .then(result => { + t.deepEqual(result, [ + { + _id: '52b213b38594d8a2be17c780', + _object: { + _id: '52b213b38594d8a2be17c780', + board_approval_month: 'November', + totalamt: 130000000 + } + }, + { _id: '52b213b38594d8a2be17c781', _object: null }, + { _id: '52b213b38594d8a2be17c782', _object: null } + ]) + }) }) test('can GET with object having deleted two docs', t => { t.plan(1) - global[indexName].GET({ - FIELD: 'board_approval_month', - VALUE: { - GTE: 'November', - LTE: 'November' - } - }) + global[indexName] + .GET({ + FIELD: 'board_approval_month', + VALUE: { + GTE: 'November', + LTE: 'November' + } + }) .then(result => { t.deepEqual(result, [ { diff --git a/test/src/DISTINCT-test.js b/test/src/DISTINCT-test.js index 6baf044..13d5df1 100644 --- a/test/src/DISTINCT-test.js +++ b/test/src/DISTINCT-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'DISTINCT' @@ -99,7 +101,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/EXIST-test.js b/test/src/EXIST-test.js index f3bc10a..94579a1 100644 --- a/test/src/EXIST-test.js +++ b/test/src/EXIST-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'EXIST' @@ -59,7 +61,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/EXPORT-test.js b/test/src/EXPORT-test.js index 7a40cf8..89560bd 100644 --- a/test/src/EXPORT-test.js +++ b/test/src/EXPORT-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const exportingIndexName = sandbox + 'EXPORT' @@ -75,7 +77,7 @@ const data = [ test('create an index for export', t => { t.plan(1) - fii({ name: exportingIndexName }).then(db => { + new InvertedIndex({ name: exportingIndexName }).then(db => { global[exportingIndexName] = db t.ok(db, !undefined) }) @@ -98,7 +100,7 @@ test('can export some data', t => { test('create an index for export', t => { t.plan(1) - fii({ name: importingIndexName }).then(db => { + new InvertedIndex({ name: importingIndexName }).then(db => { global[importingIndexName] = db t.ok(db, !undefined) }) diff --git a/test/src/FACETS-test.js b/test/src/FACETS-test.js index f2b441b..322f02e 100644 --- a/test/src/FACETS-test.js +++ b/test/src/FACETS-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'FACETS' @@ -99,7 +101,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/FIELDS-test.js b/test/src/FIELDS-test.js index 5d2f96c..7f6ac73 100644 --- a/test/src/FIELDS-test.js +++ b/test/src/FIELDS-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'FIELDS' @@ -99,7 +101,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) @@ -112,10 +114,16 @@ test('can add some data', t => { test('can show the fields', t => { t.plan(1) - global[indexName].FIELDS().then(fields => - t.deepEqual( - fields, - ['colour', 'drivetrain', 'make', 'model', 'price', 'year'] + global[indexName] + .FIELDS() + .then(fields => + t.deepEqual(fields, [ + 'colour', + 'drivetrain', + 'make', + 'model', + 'price', + 'year' + ]) ) - ) }) diff --git a/test/src/GET-test.js b/test/src/GET-test.js index e10f286..6d2e142 100644 --- a/test/src/GET-test.js +++ b/test/src/GET-test.js @@ -1,6 +1,8 @@ -const fii = require('../../') -const test = require('tape') -const sw = require('stopword') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' +import sw from 'stopword' const sandbox = 'test/sandbox/' const indexName = sandbox + 'GET' @@ -99,10 +101,12 @@ const data = [ ] test('some simple GETs', async function (t) { - const { GET, PUT } = await fii({ + const { GET, PUT } = await new InvertedIndex({ name: indexName, isLeaf: item => - typeof item === 'string' || typeof item === 'number' || Array.isArray(item) + typeof item === 'string' || + typeof item === 'number' || + Array.isArray(item) }) t.pass('db initialized') @@ -124,7 +128,7 @@ test('some simple GETs', async function (t) { }) test('testing single query replacement', async function (t) { - const { GET, PUT } = await fii({ + const { GET, PUT } = await new InvertedIndex({ name: indexName + '_1', queryReplace: { swedemachine: ['Volvo'] @@ -151,7 +155,7 @@ test('testing single query replacement', async function (t) { }) test('testing query with multiple replacements', async function (t) { - const { GET, PUT, OR } = await fii({ + const { GET, PUT, OR } = await new InvertedIndex({ name: indexName + '_2', queryReplace: { eurocars: ['Volvo', 'BMW', 'Opel'] @@ -186,7 +190,7 @@ test('testing query with multiple replacements', async function (t) { }) test('testing case sensitivity', async function (t) { - const { GET, PUT } = await fii({ + const { GET, PUT } = await new InvertedIndex({ name: indexName + '_3', caseSensitive: true }) @@ -205,7 +209,7 @@ test('testing case sensitivity', async function (t) { }) test('testing stopwords, empty and non-existant tokens', async function (t) { - const { GET, PUT } = await fii({ + const { GET, PUT } = await new InvertedIndex({ name: indexName + '_4', stopwords: sw.eng }) diff --git a/test/src/LAST_UPDATED-test.js b/test/src/LAST_UPDATED-test.js index 2a47eb3..18b97b1 100644 --- a/test/src/LAST_UPDATED-test.js +++ b/test/src/LAST_UPDATED-test.js @@ -1,6 +1,7 @@ -const fii = require('../../') -const levelOptions = require('../../src/options.js') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'LAST_UPDATED' @@ -9,7 +10,7 @@ let timestamp test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) @@ -17,11 +18,10 @@ test('create index', t => { test('LAST_UPDATED timestamp was created', t => { t.plan(1) - global[indexName].STORE.get(['~LAST_UPDATED'], levelOptions) - .then(created => { - timestamp = created - return t.pass('LAST_UPDATED timestamp created ' + timestamp) - }) + global[indexName].STORE.get(['~LAST_UPDATED']).then(created => { + timestamp = created + return t.pass('LAST_UPDATED timestamp created ' + timestamp) + }) }) test('can read LAST_UPDATED timestamp with API', t => { @@ -31,10 +31,15 @@ test('can read LAST_UPDATED timestamp with API', t => { test('when adding a new doc, LAST_UPDATE increments', t => { t.plan(1) - setTimeout(function () { // wait to ensure that newer timestamp is bigger - global[indexName].PUT([{ - text: 'this is a new doc'.split() - }]).then(global[indexName].LAST_UPDATED) + setTimeout(function () { + // wait to ensure that newer timestamp is bigger + global[indexName] + .PUT([ + { + text: 'this is a new doc'.split() + } + ]) + .then(global[indexName].LAST_UPDATED) .then(newTimestamp => t.ok(newTimestamp > timestamp)) }, 100) }) diff --git a/test/src/MAX-MIN-test.js b/test/src/MAX-MIN-test.js index ef2feb2..c99e94b 100644 --- a/test/src/MAX-MIN-test.js +++ b/test/src/MAX-MIN-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'MAXMIN' @@ -99,10 +101,12 @@ const data = [ test('create index', t => { t.plan(1) - fii({ + new InvertedIndex({ name: indexName, isLeaf: item => - typeof item === 'string' || typeof item === 'number' || Array.isArray(item) + typeof item === 'string' || + typeof item === 'number' || + Array.isArray(item) }).then(db => { global[indexName] = db t.ok(db, !undefined) diff --git a/test/src/OR-test.js b/test/src/OR-test.js index b61d964..bea4204 100644 --- a/test/src/OR-test.js +++ b/test/src/OR-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' @@ -97,7 +99,9 @@ const data = [ ] test('simple OR', async t => { - const { AND, GET, PUT, OR } = await fii({ name: sandbox + 'OR' }) + const { AND, GET, PUT, OR } = await new InvertedIndex({ + name: sandbox + 'OR' + }) t.deepEquals(await PUT(data), [ { _id: 0, operation: 'PUT', status: 'CREATED' }, { _id: 1, operation: 'PUT', status: 'CREATED' }, diff --git a/test/src/PUT-test.js b/test/src/PUT-test.js index 7e2b424..caf9e40 100644 --- a/test/src/PUT-test.js +++ b/test/src/PUT-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'PUT' @@ -30,7 +32,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/aggregation-test.js b/test/src/aggregation-test.js index 6545a47..fd143cd 100644 --- a/test/src/aggregation-test.js +++ b/test/src/aggregation-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'cars-aggregation-test' @@ -99,7 +101,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/case-sensitive-test.js b/test/src/case-sensitive-test.js index cdf9daf..602f4a6 100644 --- a/test/src/case-sensitive-test.js +++ b/test/src/case-sensitive-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' // TODO: why does 'case-sensitive-test' break everything here? @@ -8,7 +10,7 @@ const caseInsensitiveIdx = sandbox + 'case-insensitive-test' test('create a case sensitive index', t => { t.plan(1) - fii({ + new InvertedIndex({ name: caseSensitiveIdx, caseSensitive: true }).then(db => { @@ -19,7 +21,7 @@ test('create a case sensitive index', t => { test('create a case INsensitive index', t => { t.plan(1) - fii({ + new InvertedIndex({ name: caseInsensitiveIdx, caseSensitive: false }).then(db => { diff --git a/test/src/character-normalization-test.js b/test/src/character-normalization-test.js index b6efee3..0fb99c2 100644 --- a/test/src/character-normalization-test.js +++ b/test/src/character-normalization-test.js @@ -1,11 +1,14 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' +import diacritic from 'diacritic' + const sandbox = 'test/sandbox/' const indexName = sandbox + 'charnorm' -const diacritic = require('diacritic') test('some simple GETs', async function (t) { - const { GET, OR, PUT } = await fii({ name: indexName }) + const { GET, OR, PUT } = await new InvertedIndex({ name: indexName }) t.pass('db initialized') await PUT( diff --git a/test/src/dont-index-certain-fields-test.js b/test/src/dont-index-certain-fields-test.js index cd38e3a..fdef530 100644 --- a/test/src/dont-index-certain-fields-test.js +++ b/test/src/dont-index-certain-fields-test.js @@ -1,8 +1,9 @@ -const fii = require('../../') -const levelOptions = require('../../src/options.js') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) -const test = require('tape') -const { EntryStream } = require('level-read-stream') +import test from 'tape' +import { EntryStream } from 'level-read-stream' const sandbox = 'test/sandbox/' @@ -10,7 +11,7 @@ const indexName = sandbox + 'dont-index-certain-fields' test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) @@ -92,7 +93,9 @@ test('analyse index', t => { { key: ['IDX', 'make', ['Tesla']], value: ['0', '2'] } ] t.plan(storeState.length) - const r = new EntryStream(global[indexName].STORE, { lt: ['~'], ...levelOptions }) + const r = new EntryStream(global[indexName].STORE, { + lt: ['~'] + }) r.on('data', d => t.deepEqual(d, storeState.shift())) }) @@ -100,7 +103,7 @@ const indexName2 = sandbox + 'non-searchable-fields-test2' test('create another index', t => { t.plan(1) - fii({ name: indexName2 }).then(db => { + new InvertedIndex({ name: indexName2 }).then(db => { global[indexName2] = db t.ok(db, !undefined) }) @@ -180,6 +183,8 @@ test('analyse index', t => { { key: ['IDX', 'make', ['Tesla']], value: ['0', '2'] } ] t.plan(storeState.length) - const r = new EntryStream(global[indexName2].STORE, { lt: ['~'], ...levelOptions }) + const r = new EntryStream(global[indexName2].STORE, { + lt: ['~'] + }) r.on('data', d => t.deepEqual(d, storeState.shift())) }) diff --git a/test/src/indexing-arrays-test.js b/test/src/indexing-arrays-test.js index 4bb1307..dfdbeb3 100644 --- a/test/src/indexing-arrays-test.js +++ b/test/src/indexing-arrays-test.js @@ -1,7 +1,8 @@ -const fii = require('../../') -const levelOptions = require('../../src/options.js') -const test = require('tape') -const { EntryStream } = require('level-read-stream') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' +import { EntryStream } from 'level-read-stream' const sandbox = 'test/sandbox/' const indexName = sandbox + 'indexing-arrays-test' @@ -58,7 +59,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) @@ -85,8 +86,7 @@ test('fields are indexed correctly when there are nested arrays involved', t => t.plan(expected.length) new EntryStream(global[indexName].STORE, { gte: ['FIELD', ''], - lte: ['FIELD', '○'], - ...levelOptions + lte: ['FIELD', '○'] }).on('data', d => t.deepEqual(d, expected.shift())) }) @@ -124,7 +124,6 @@ test('tokens are indexed correctly when there are nested arrays involved', t => t.plan(expected.length) new EntryStream(global[indexName].STORE, { gte: ['IDX'], - lte: ['IDX', '○'], - ...levelOptions + lte: ['IDX', '○'] }).on('data', d => t.deepEqual(d, expected.shift())) }) diff --git a/test/src/indexing-without-ids-test.js b/test/src/indexing-without-ids-test.js index 2de0fb2..9cf6289 100644 --- a/test/src/indexing-without-ids-test.js +++ b/test/src/indexing-without-ids-test.js @@ -1,14 +1,15 @@ -const fii = require('../../') -const levelOptions = require('../../src/options.js') -const test = require('tape') -const { EntryStream } = require('level-read-stream') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' +import { EntryStream } from 'level-read-stream' const sandbox = 'test/sandbox/' const indexName = sandbox + 'indexing-without-ids-test' test('create another index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) @@ -46,7 +47,7 @@ test('can GET with string', t => { { key: ['IDX', 'land', ['SCOTLAND']], value: [1] } ] t.plan(result.length) - new EntryStream(global[indexName].STORE, { lt: ['~'], ...levelOptions }).on('data', d => + new EntryStream(global[indexName].STORE, { lt: ['~'] }).on('data', d => t.deepEqual(d, result.shift()) ) }) diff --git a/test/src/init-test.js b/test/src/init-test.js new file mode 100644 index 0000000..1a67edf --- /dev/null +++ b/test/src/init-test.js @@ -0,0 +1,80 @@ +import test from 'tape' + +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) + +// import { InvertedIndex } from '../../src/browser.js' + +console.log('boooooom') + +const sandbox = 'test/sandbox/' +const indexName = sandbox + 'init' +const global = {} + +const data = [ + { + _id: 'a', + title: 'quite a cool document', + body: { + text: 'this document is really cool cool cool', + metadata: 'coolness documentness' + }, + importantNumber: 5000 + }, + { + _id: 'b', + title: 'quite a cool document', + body: { + text: 'this document is really cool bananas', + metadata: 'coolness documentness' + }, + importantNumber: 500 + }, + { + _id: 'c', + title: 'something different', + body: { + text: 'something totally different', + metadata: 'coolness documentness' + }, + importantNumber: 200 + } +] + +test('create index', t => { + t.plan(1) + new InvertedIndex({ name: indexName }).then(db => { + global[indexName] = db + t.ok(db, !undefined) + }) +}) + +test('can add some data', t => { + t.plan(1) + global[indexName] + .PUT([ + { + _id: 1, + colour: 'Black', + drivetrain: 'Diesel' + } + ]) + .then(t.pass) +}) + +test('get simple AND', t => { + t.plan(1) + + global[indexName].AND(['drivetrain:Diesel', 'colour:Black']).then(result => + t.deepEqual(result, [ + { + _id: 1, + _match: [ + { FIELD: 'colour', VALUE: 'Black' }, + { FIELD: 'drivetrain', VALUE: 'Diesel' } + ] + } + ]) + ) +}) diff --git a/test/src/memory-level-test.js b/test/src/memory-level-test.js index a8e3724..7e74a45 100644 --- a/test/src/memory-level-test.js +++ b/test/src/memory-level-test.js @@ -1,7 +1,9 @@ -const fii = require('../../src/main') -const { MemoryLevel } = require('memory-level') -const test = require('tape') -const wbd = require('world-bank-dataset') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import { MemoryLevel } from 'memory-level' +import test from 'tape' +import wbd from 'world-bank-dataset' const data = wbd.slice(0, 10).map(item => { return { @@ -18,7 +20,7 @@ const data = wbd.slice(0, 10).map(item => { test('create a fii with memory-level', t => { t.plan(2) - fii({ + new InvertedIndex({ db: new MemoryLevel() }).then(db => db diff --git a/test/src/parseToken-test.js b/test/src/parseToken-test.js index a7bff50..808e064 100644 --- a/test/src/parseToken-test.js +++ b/test/src/parseToken-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'parseToken' @@ -99,7 +101,7 @@ const data = [ test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/query-parser-test.js b/test/src/query-parser-test.js index bd9b53d..d697414 100644 --- a/test/src/query-parser-test.js +++ b/test/src/query-parser-test.js @@ -1,13 +1,15 @@ -const fii = require('../../') -const test = require('tape') -const wbd = require('world-bank-dataset') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' +import wbd from 'world-bank-dataset' const sandbox = 'test/sandbox/' const indexName = sandbox + 'wb2' test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/score-test.js b/test/src/score-test.js index 64c2914..43128cd 100644 --- a/test/src/score-test.js +++ b/test/src/score-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'score' @@ -106,10 +108,12 @@ const data = [ test('create index', t => { t.plan(1) - fii({ + new InvertedIndex({ name: indexName, isLeaf: item => - typeof item === 'string' || typeof item === 'number' || Array.isArray(item) + typeof item === 'string' || + typeof item === 'number' || + Array.isArray(item) }).then(db => { global[indexName] = db t.ok(db, !undefined) diff --git a/test/src/search-in-all-fields-test.js b/test/src/search-in-all-fields-test.js index d1e467b..38db92f 100644 --- a/test/src/search-in-all-fields-test.js +++ b/test/src/search-in-all-fields-test.js @@ -1,12 +1,14 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'search-in-all-fields' test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/stemmer-test.js b/test/src/stemmer-test.js index 561b8ba..d097876 100644 --- a/test/src/stemmer-test.js +++ b/test/src/stemmer-test.js @@ -1,5 +1,7 @@ -const fii = require('../../') -const test = require('tape') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' const sandbox = 'test/sandbox/' const indexName = sandbox + 'stemmer' @@ -7,7 +9,7 @@ const indexName = sandbox + 'stemmer' test('init fii and index data', async t => { const { stemmer } = await import('stemmer') - const { DELETE, GET, PUT, QUERY_PIPELINE_STAGES } = await fii({ + const { DELETE, GET, PUT, QUERY_PIPELINE_STAGES } = await new InvertedIndex({ name: indexName }) @@ -18,7 +20,6 @@ test('init fii and index data', async t => { 'I am running away', 'I ran up the stairs' ].map(s => s.split(' ')) - // .map(line => line.map(stemmer)) t.deepEquals(await PUT(data), [ { _id: 1, operation: 'PUT', status: 'CREATED' }, @@ -60,16 +61,11 @@ test('init fii and index data', async t => { ]) t.deepEquals( - await GET('walked', [ - ({ token, ops }) => { - token.VALUE.GTE = stemmer(token.VALUE.GTE) - token.VALUE.LTE = stemmer(token.VALUE.LTE) - return { - token, - ops - } - } - ]), + await GET('walked', token => { + token.VALUE.GTE = stemmer(token.VALUE.GTE) + token.VALUE.LTE = stemmer(token.VALUE.LTE) + return token + }), [ { _id: 6, _match: [{ FIELD: '', VALUE: 'walk' }] }, { _id: 7, _match: [{ FIELD: '', VALUE: 'walk' }] }, diff --git a/test/src/stopword-test.js b/test/src/stopword-test.js index 0649b54..e822867 100644 --- a/test/src/stopword-test.js +++ b/test/src/stopword-test.js @@ -1,14 +1,15 @@ -const fii = require('../../') -const levelOptions = require('../../src/options.js') -const test = require('tape') -const { EntryStream } = require('level-read-stream') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' +import { EntryStream } from 'level-read-stream' const sandbox = 'test/sandbox/' const indexName = sandbox + 'stopword-test' test('create index', t => { t.plan(1) - fii({ + new InvertedIndex({ name: indexName, stopwords: ['this', 'is', 'a', 'that', 'bananas'] }).then(db => { @@ -47,7 +48,7 @@ test('can verify store', t => { { key: ['IDX', 'text', ['sentence']], value: [0, 1] } ] t.plan(entries.length + 1) - new EntryStream(global[indexName].STORE, { lt: ['~'], ...levelOptions }) + new EntryStream(global[indexName].STORE, { lt: ['~'] }) .on('data', d => t.deepEquals(d, entries.shift())) .on('end', resolve => t.pass('ended')) }) diff --git a/test/src/store-vector-test.js b/test/src/store-vector-test.js index d5735b6..d5dfef7 100644 --- a/test/src/store-vector-test.js +++ b/test/src/store-vector-test.js @@ -1,14 +1,15 @@ -const fii = require('../../') -const levelOptions = require('../../src/options.js') -const test = require('tape') -const { EntryStream } = require('level-read-stream') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' +import { EntryStream } from 'level-read-stream' const sandbox = 'test/sandbox/' const indexName = sandbox + 'store-vector-test' test('create index', t => { t.plan(1) - fii({ + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db @@ -46,7 +47,7 @@ test('can verify store', t => { { key: ['IDX', 'text', ['this']], value: [0] } ] t.plan(entries.length + 1) - new EntryStream(global[indexName].STORE, { lt: ['~'], ...levelOptions }) + new EntryStream(global[indexName].STORE, { lt: ['~'] }) .on('data', d => t.deepEquals(d, entries.shift())) .on('end', resolve => t.pass('ended')) }) diff --git a/test/src/stress-test.js b/test/src/stress-test.js index ec85945..2d4b2ec 100644 --- a/test/src/stress-test.js +++ b/test/src/stress-test.js @@ -1,13 +1,15 @@ -const fii = require('../../') -const test = require('tape') -const wbd = require('world-bank-dataset') +const { InvertedIndex } = await import( + '../../src/' + process.env.FII_ENTRYPOINT +) +import test from 'tape' +import wbd from 'world-bank-dataset' const sandbox = 'test/sandbox/' const indexName = sandbox + 'stress-test' test('create index', t => { t.plan(1) - fii({ name: indexName }).then(db => { + new InvertedIndex({ name: indexName }).then(db => { global[indexName] = db t.ok(db, !undefined) }) diff --git a/test/src/test-test.js b/test/src/test-test.js new file mode 100644 index 0000000..a94d256 --- /dev/null +++ b/test/src/test-test.js @@ -0,0 +1,10 @@ +import test from 'tape' + +// import { InvertedIndex } from '../../src/browser.js' + +const { InvertedIndex } = await import('../../src/browser.js') + +test('simple test', t => { + t.plan(1) + t.ok('this test passes') +}) diff --git a/webpack.config.js b/webpack.config.js index 1db1122..6aa32d1 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,50 +1,56 @@ -const path = require('path') -const webpack = require('webpack') -const glob = require("glob"); +import glob from 'glob' +import path from 'path' +import webpack from 'webpack' +import { createRequire } from 'node:module' + +const require = createRequire(import.meta.url) const config = { plugins: [ // Webpack 5 no longer polyfills 'process' new webpack.ProvidePlugin({ - process: 'process/browser', + process: 'process/browser.js' }), // as per https://github.com/webpack/changelog-v5/issues/10 new webpack.ProvidePlugin({ - Buffer: ['buffer', 'Buffer'], + Buffer: ['buffer', 'Buffer'] }) ], - target: 'web', + target: ['web'], resolve: { fallback: { // BREAKING CHANGE: webpack < 5 used to include polyfills for // node.js core modules by default. This is no longer the // case. - 'assert': false, - "buffer": require.resolve("buffer/"), - "fs": false, - "path": require.resolve("path-browserify"), - "stream": require.resolve("stream-browserify"), - 'util': false, - 'os': 'os-browserify/browser', + assert: false, + buffer: require.resolve('buffer/'), + fs: false, + path: require.resolve('path-browserify'), + stream: require.resolve('stream-browserify'), + util: false, + os: require.resolve('os-browserify') } } } -module.exports = [{ - ...config, - mode: 'production', - entry: './src/main.js', - output: { - path: path.resolve('dist'), - filename: 'fergies-inverted-index.js', - library: 'FergiesInvertedIndex' - } -}, { - ...config, - mode: 'development', - entry: glob.sync('./test/src/*-test.js'), - output: { - path: path.resolve('test/sandbox'), - filename: 'browser-tests.js' +export default [ + { + ...config, + mode: 'production', + entry: './src/browser.js', + output: { + path: path.resolve('dist'), + filename: 'fergies-inverted-index.js', + library: 'FergiesInvertedIndex' + } }, -}] + { + ...config, + mode: 'production', + entry: glob.sync('./test/src/test-test.js'), + output: { + path: path.resolve('test/sandbox'), + filename: 'browser-tests.js' + } + } +]