From 45168c2c6b422f7001dcd4fc614cf328e244f298 Mon Sep 17 00:00:00 2001 From: yoshinorin Date: Tue, 14 Jun 2022 23:50:49 +0900 Subject: [PATCH] refactor: migrate to ESM --- lib/database.js | 28 ++++++++++++------------- lib/document.js | 7 +++---- lib/error.js | 4 +--- lib/error/population.js | 6 ++---- lib/error/validation.js | 6 ++---- lib/model.js | 27 ++++++++++++------------ lib/mutex.js | 4 +--- lib/query.js | 8 +++---- lib/schema.js | 16 +++++++------- lib/schematype.js | 8 +++---- lib/types/array.js | 8 +++---- lib/types/boolean.js | 8 +++---- lib/types/buffer.js | 8 +++---- lib/types/cuid.js | 10 ++++----- lib/types/date.js | 8 +++---- lib/types/enum.js | 8 +++---- lib/types/index.js | 39 +++++++++++++++++++++++------------ lib/types/integer.js | 8 +++---- lib/types/number.js | 8 +++---- lib/types/object.js | 6 ++---- lib/types/string.js | 8 +++---- lib/types/virtual.js | 8 +++---- lib/util.js | 38 ++++++++++++++++------------------ package.json | 5 +++-- test/scripts/database.js | 25 ++++++++++++---------- test/scripts/document.js | 10 ++++----- test/scripts/model.js | 17 ++++++++------- test/scripts/mutex.js | 9 ++++---- test/scripts/query.js | 15 +++++++------- test/scripts/schema.js | 7 +++---- test/scripts/schematype.js | 9 ++++---- test/scripts/types/array.js | 16 +++++++------- test/scripts/types/boolean.js | 9 ++++---- test/scripts/types/buffer.js | 9 ++++---- test/scripts/types/cuid.js | 9 ++++---- test/scripts/types/date.js | 9 ++++---- test/scripts/types/enum.js | 10 ++++----- test/scripts/types/integer.js | 9 ++++---- test/scripts/types/number.js | 9 ++++---- test/scripts/types/object.js | 7 +++---- test/scripts/types/string.js | 9 ++++---- test/scripts/types/virtual.js | 7 +++---- test/scripts/util.js | 8 +++---- 43 files changed, 225 insertions(+), 262 deletions(-) diff --git a/lib/database.js b/lib/database.js index f17b7d45..94f2e838 100644 --- a/lib/database.js +++ b/lib/database.js @@ -1,16 +1,16 @@ -'use strict'; - -const JSONStream = require('JSONStream'); -const Promise = require('bluebird'); -const fs = require('graceful-fs'); -const Model = require('./model'); -const Schema = require('./schema'); -const SchemaType = require('./schematype'); -const WarehouseError = require('./error'); -const pkg = require('../package.json'); +import JSONStream from 'JSONStream'; +import Promise from 'bluebird'; +import fs from 'graceful-fs'; +import Model from './model.js'; +import Schema from './schema.js'; +import SchemaType from './schematype.js'; +import WarehouseError from './error.js'; +import pkg from '../package.json' assert { type: "json" }; +import log from 'hexo-log'; +import { Stream } from 'stream'; + +const pipeline = Promise.promisify(Stream.pipeline); const { open } = fs.promises; -const pipeline = Promise.promisify(require('stream').pipeline); -const log = require('hexo-log')(); let _writev; @@ -69,7 +69,7 @@ async function exportAsync(database, path) { } } -class Database { +export default class Database { /** * Database constructor. @@ -190,4 +190,4 @@ Database.prototype.SchemaType = SchemaType; Database.SchemaType = Database.prototype.SchemaType; Database.version = pkg.version; -module.exports = Database; +// export default Database; \ No newline at end of file diff --git a/lib/document.js b/lib/document.js index fd8f672b..fdafbb55 100644 --- a/lib/document.js +++ b/lib/document.js @@ -1,6 +1,5 @@ -'use strict'; - -const cloneDeep = require('rfdc')(); +import rfdc from 'rfdc'; +const cloneDeep = rfdc(); class Document { @@ -101,4 +100,4 @@ function isGetter(obj, key) { return Object.getOwnPropertyDescriptor(obj, key).get; } -module.exports = Document; +export default Document; diff --git a/lib/error.js b/lib/error.js index 1e097423..224b571a 100644 --- a/lib/error.js +++ b/lib/error.js @@ -1,5 +1,3 @@ -'use strict'; - class WarehouseError extends Error { /** @@ -22,4 +20,4 @@ WarehouseError.ID_EXIST = 'ID_EXIST'; WarehouseError.ID_NOT_EXIST = 'ID_NOT_EXIST'; WarehouseError.ID_UNDEFINED = 'ID_UNDEFINED'; -module.exports = WarehouseError; +export default WarehouseError; diff --git a/lib/error/population.js b/lib/error/population.js index 6e72e928..9715915f 100644 --- a/lib/error/population.js +++ b/lib/error/population.js @@ -1,9 +1,7 @@ -'use strict'; - -const WarehouseError = require('../error'); +import WarehouseError from '../error.js'; class PopulationError extends WarehouseError {} PopulationError.prototype.name = 'PopulationError'; -module.exports = PopulationError; +export default PopulationError; diff --git a/lib/error/validation.js b/lib/error/validation.js index f3f02efe..d847ce9d 100644 --- a/lib/error/validation.js +++ b/lib/error/validation.js @@ -1,9 +1,7 @@ -'use strict'; - -const WarehouseError = require('../error'); +import WarehouseError from '../error.js'; class ValidationError extends WarehouseError {} ValidationError.prototype.name = 'ValidationError'; -module.exports = ValidationError; +export default ValidationError; diff --git a/lib/model.js b/lib/model.js index 0e19c4af..878bda1c 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1,16 +1,15 @@ -'use strict'; - -const { EventEmitter } = require('events'); -const cloneDeep = require('rfdc')(); -const Promise = require('bluebird'); -const { parseArgs, getProp, setGetter, shuffle } = require('./util'); -const Document = require('./document'); -const Query = require('./query'); -const Schema = require('./schema'); -const Types = require('./types'); -const WarehouseError = require('./error'); -const PopulationError = require('./error/population'); -const Mutex = require('./mutex'); +import { EventEmitter } from 'events'; +import rfdc from 'rfdc'; +const cloneDeep = rfdc(); +import Promise from 'bluebird'; +import { parseArgs, getProp, setGetter, shuffle } from './util.js'; +import Document from './document.js'; +import Query from './query.js'; +import Schema from './schema.js'; +import * as Types from './types/index.js'; +import WarehouseError from './error.js'; +import PopulationError from './error/population.js'; +import Mutex from './mutex.js'; class Model extends EventEmitter { @@ -963,4 +962,4 @@ Model.prototype.each = Model.prototype.forEach; Model.prototype.random = Model.prototype.shuffle; -module.exports = Model; +export default Model; diff --git a/lib/mutex.js b/lib/mutex.js index 87725b34..ee628350 100644 --- a/lib/mutex.js +++ b/lib/mutex.js @@ -1,5 +1,3 @@ -'use strict'; - class Mutex { constructor() { this._locked = false; @@ -29,4 +27,4 @@ class Mutex { } } -module.exports = Mutex; +export default Mutex; diff --git a/lib/query.js b/lib/query.js index 53b94881..511a6130 100644 --- a/lib/query.js +++ b/lib/query.js @@ -1,7 +1,5 @@ -'use strict'; - -const Promise = require('bluebird'); -const { parseArgs, shuffle } = require('./util'); +import Promise from 'bluebird'; +import { parseArgs, shuffle } from './util.js'; class Query { @@ -387,4 +385,4 @@ Query.prototype.each = Query.prototype.forEach; Query.prototype.random = Query.prototype.shuffle; -module.exports = Query; +export default Query; diff --git a/lib/schema.js b/lib/schema.js index a683d8d5..43ec5cf1 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -1,11 +1,9 @@ -'use strict'; - -const SchemaType = require('./schematype'); -const Types = require('./types'); -const Promise = require('bluebird'); -const { getProp, setProp, delProp } = require('./util'); -const PopulationError = require('./error/population'); -const { isPlainObject } = require('is-plain-object'); +import SchemaType from './schematype.js'; +import * as Types from './types/index.js'; +import Promise from 'bluebird'; +import { getProp, setProp, delProp } from './util.js'; +import PopulationError from './error/population.js'; +import { isPlainObject } from 'is-plain-object'; /** * @callback queryFilterCallback @@ -792,4 +790,4 @@ class Schema { Schema.prototype.Types = Types; Schema.Types = Schema.prototype.Types; -module.exports = Schema; +export default Schema; diff --git a/lib/schematype.js b/lib/schematype.js index 06192a30..e4a93b3e 100644 --- a/lib/schematype.js +++ b/lib/schematype.js @@ -1,7 +1,5 @@ -'use strict'; - -const { setProp } = require('./util'); -const ValidationError = require('./error/validation'); +import { setProp } from './util.js'; +import ValidationError from './error/validation.js'; /** * This is the basic schema type. @@ -292,4 +290,4 @@ SchemaType.prototype.q$max = SchemaType.prototype.q$lte; SchemaType.prototype.q$min = SchemaType.prototype.q$gte; -module.exports = SchemaType; +export default SchemaType; diff --git a/lib/types/array.js b/lib/types/array.js index 9c7b08ab..a9351352 100644 --- a/lib/types/array.js +++ b/lib/types/array.js @@ -1,7 +1,5 @@ -'use strict'; - -const SchemaType = require('../schematype'); -const ValidationError = require('../error/validation'); +import SchemaType from '../schematype.js'; +import ValidationError from '../error/validation.js'; const { isArray } = Array; @@ -378,4 +376,4 @@ SchemaTypeArray.prototype.u$append = SchemaTypeArray.prototype.u$push; SchemaTypeArray.prototype.u$prepend = SchemaTypeArray.prototype.u$unshift; -module.exports = SchemaTypeArray; +export default SchemaTypeArray; diff --git a/lib/types/boolean.js b/lib/types/boolean.js index 16148b1c..7be7115f 100644 --- a/lib/types/boolean.js +++ b/lib/types/boolean.js @@ -1,7 +1,5 @@ -'use strict'; - -const SchemaType = require('../schematype'); -const ValidationError = require('../error/validation'); +import SchemaType from '../schematype.js'; +import ValidationError from '../error/validation.js'; /** * Boolean schema type. @@ -63,4 +61,4 @@ class SchemaTypeBoolean extends SchemaType { } } -module.exports = SchemaTypeBoolean; +export default SchemaTypeBoolean; diff --git a/lib/types/buffer.js b/lib/types/buffer.js index 20df46b5..c5111602 100644 --- a/lib/types/buffer.js +++ b/lib/types/buffer.js @@ -1,7 +1,5 @@ -'use strict'; - -const SchemaType = require('../schematype'); -const ValidationError = require('../error/validation'); +import SchemaType from '../schematype.js'; +import ValidationError from '../error/validation.js'; /** * Boolean schema type. @@ -107,4 +105,4 @@ class SchemaTypeBuffer extends SchemaType { } } -module.exports = SchemaTypeBuffer; +export default SchemaTypeBuffer; diff --git a/lib/types/cuid.js b/lib/types/cuid.js index 4e723e89..7d3dbe1b 100644 --- a/lib/types/cuid.js +++ b/lib/types/cuid.js @@ -1,8 +1,6 @@ -'use strict'; - -const SchemaType = require('../schematype'); -const cuid = require('cuid'); -const ValidationError = require('../error/validation'); +import SchemaType from '../schematype.js'; +import cuid from 'cuid'; +import ValidationError from '../error/validation.js'; /** * [CUID](https://github.com/ericelliott/cuid) schema type. @@ -41,4 +39,4 @@ class SchemaTypeCUID extends SchemaType { } } -module.exports = SchemaTypeCUID; +export default SchemaTypeCUID; diff --git a/lib/types/date.js b/lib/types/date.js index a17e7c05..1202cebf 100644 --- a/lib/types/date.js +++ b/lib/types/date.js @@ -1,7 +1,5 @@ -'use strict'; - -const SchemaType = require('../schematype'); -const ValidationError = require('../error/validation'); +import SchemaType from '../schematype.js'; +import ValidationError from '../error/validation.js'; /** * Date schema type. @@ -154,4 +152,4 @@ class SchemaTypeDate extends SchemaType { } } -module.exports = SchemaTypeDate; +export default SchemaTypeDate; diff --git a/lib/types/enum.js b/lib/types/enum.js index e720507b..04c87bd9 100644 --- a/lib/types/enum.js +++ b/lib/types/enum.js @@ -1,7 +1,5 @@ -'use strict'; - -const SchemaType = require('../schematype'); -const ValidationError = require('../error/validation'); +import SchemaType from '../schematype.js'; +import ValidationError from '../error/validation.js'; /** * Enum schema type. @@ -41,4 +39,4 @@ class SchemaTypeEnum extends SchemaType { } } -module.exports = SchemaTypeEnum; +export default SchemaTypeEnum; diff --git a/lib/types/index.js b/lib/types/index.js index d2bb02f6..997bb6ab 100644 --- a/lib/types/index.js +++ b/lib/types/index.js @@ -1,14 +1,27 @@ -'use strict'; +import SchemaType from '../schematype.js'; +import SchemaTypeString from './string.js'; +import SchemaTypeNumber from './number.js'; +import SchemaTypeBoolean from './boolean.js'; +import SchemaTypeArray from './array.js'; +import SchemaTypeObject from './object.js'; +import SchemaTypeDate from './date.js'; +import SchemaTypeVirtual from './virtual.js'; +import SchemaTypeCUID from './cuid.js'; +import SchemaTypeEnum from './enum.js'; +import SchemaTypeInteger from './integer.js'; +import SchemaTypeBuffer from './buffer.js'; -exports.Mixed = require('../schematype'); -exports.String = require('./string'); -exports.Number = require('./number'); -exports.Boolean = require('./boolean'); -exports.Array = require('./array'); -exports.Object = require('./object'); -exports.Date = require('./date'); -exports.Virtual = require('./virtual'); -exports.CUID = require('./cuid'); -exports.Enum = require('./enum'); -exports.Integer = require('./integer'); -exports.Buffer = require('./buffer'); +export { + SchemaType as Mixed, + SchemaTypeString as String, + SchemaTypeNumber as Number, + SchemaTypeBoolean as Boolean, + SchemaTypeArray as Array, + SchemaTypeObject as Object, + SchemaTypeDate as Date, + SchemaTypeVirtual as Virtual, + SchemaTypeCUID as CUID, + SchemaTypeEnum as Enum, + SchemaTypeInteger as Integer, + SchemaTypeBuffer as Buffer +}; diff --git a/lib/types/integer.js b/lib/types/integer.js index 2c855042..6f20ad6f 100644 --- a/lib/types/integer.js +++ b/lib/types/integer.js @@ -1,7 +1,5 @@ -'use strict'; - -const SchemaTypeNumber = require('./number'); -const ValidationError = require('../error/validation'); +import SchemaTypeNumber from './number.js'; +import ValidationError from '../error/validation.js'; /** * Integer schema type. @@ -39,4 +37,4 @@ class SchemaTypeInteger extends SchemaTypeNumber { } } -module.exports = SchemaTypeInteger; +export default SchemaTypeInteger; diff --git a/lib/types/number.js b/lib/types/number.js index 90dbffa5..f641b06d 100644 --- a/lib/types/number.js +++ b/lib/types/number.js @@ -1,7 +1,5 @@ -'use strict'; - -const SchemaType = require('../schematype'); -const ValidationError = require('../error/validation'); +import SchemaType from '../schematype.js'; +import ValidationError from '../error/validation.js'; /** * Number schema type. @@ -125,4 +123,4 @@ class SchemaTypeNumber extends SchemaType { } } -module.exports = SchemaTypeNumber; +export default SchemaTypeNumber; diff --git a/lib/types/object.js b/lib/types/object.js index ff2eb391..53ce9825 100644 --- a/lib/types/object.js +++ b/lib/types/object.js @@ -1,6 +1,4 @@ -'use strict'; - -const SchemaType = require('../schematype'); +import SchemaType from '../schematype.js'; /** * Object schema type. @@ -19,4 +17,4 @@ class SchemaTypeObject extends SchemaType { } } -module.exports = SchemaTypeObject; +export default SchemaTypeObject; diff --git a/lib/types/string.js b/lib/types/string.js index ac42f089..14d21266 100644 --- a/lib/types/string.js +++ b/lib/types/string.js @@ -1,7 +1,5 @@ -'use strict'; - -const SchemaType = require('../schematype'); -const ValidationError = require('../error/validation'); +import SchemaType from '../schematype.js'; +import ValidationError from '../error/validation.js'; /** * String schema type. @@ -100,4 +98,4 @@ class SchemaTypeString extends SchemaType { } } -module.exports = SchemaTypeString; +export default SchemaTypeString; diff --git a/lib/types/virtual.js b/lib/types/virtual.js index adf8748f..dfe5cd17 100644 --- a/lib/types/virtual.js +++ b/lib/types/virtual.js @@ -1,7 +1,5 @@ -'use strict'; - -const SchemaType = require('../schematype'); -const { setGetter } = require('../util'); +import SchemaType from '../schematype.js'; +import { setGetter } from '../util.js'; /** * Virtual schema type. @@ -77,4 +75,4 @@ class SchemaTypeVirtual extends SchemaType { } } -module.exports = SchemaTypeVirtual; +export default SchemaTypeVirtual; diff --git a/lib/util.js b/lib/util.js index 596e2acd..359a834f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,4 @@ -'use strict'; - -exports.shuffle = array => { +export function shuffle(array) { if (!Array.isArray(array)) throw new TypeError('array must be an Array!'); const $array = array.slice(); const { length } = $array; @@ -17,13 +15,13 @@ exports.shuffle = array => { } return $array; -}; +} function extractPropKey(key) { return key.split('.'); } -exports.getProp = (obj, key) => { +export function getProp(obj, key) { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); @@ -40,9 +38,9 @@ exports.getProp = (obj, key) => { } return result; -}; +} -exports.setProp = (obj, key, value) => { +export function setProp(obj, key, value) { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); @@ -64,9 +62,9 @@ exports.setProp = (obj, key, value) => { } cursor[lastKey] = value; -}; +} -exports.delProp = (obj, key) => { +export function delProp(obj, key) { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); @@ -92,9 +90,9 @@ exports.delProp = (obj, key) => { } delete cursor[lastKey]; -}; +} -exports.setGetter = (obj, key, fn) => { +export function setGetter(obj, key, fn) { if (typeof obj !== 'object') throw new TypeError('obj must be an object!'); if (!key) throw new TypeError('key is required!'); if (typeof fn !== 'function') throw new TypeError('fn must be a function!'); @@ -117,9 +115,9 @@ exports.setGetter = (obj, key, fn) => { } cursor.__defineGetter__(lastKey, fn); -}; +} -exports.arr2obj = (arr, value) => { +export function arr2obj(arr, value) { if (!Array.isArray(arr)) throw new TypeError('arr must be an array!'); const obj = {}; @@ -130,9 +128,9 @@ exports.arr2obj = (arr, value) => { } return obj; -}; +} -exports.reverse = arr => { +export function reverse(arr) { if (!Array.isArray(arr)) throw new TypeError('arr must be an array!'); const len = arr.length; @@ -146,9 +144,9 @@ exports.reverse = arr => { } return arr; -}; +} -function parseArgs(args) { +function _parseArgs(args) { if (typeof args !== 'string') return args; const arr = args.split(' '); @@ -174,17 +172,17 @@ function parseArgs(args) { return result; } -exports.parseArgs = (orderby, order) => { +export function parseArgs(orderby, order) { let result; if (order) { result = {}; result[orderby] = order; } else if (typeof orderby === 'string') { - result = parseArgs(orderby); + result = _parseArgs(orderby); } else { result = orderby; } return result; -}; +} diff --git a/package.json b/package.json index 12f84394..a2763d04 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "warehouse", "version": "4.0.1", "description": "Simple JSON-based database", - "main": "lib/database", + "type": "module", + "exports": "./lib/database.js", "directories": { "lib": "./lib" }, @@ -33,7 +34,7 @@ "rfdc": "^1.1.4" }, "devDependencies": { - "chai": "^4.2.0", + "chai": "^4.3.6", "chai-as-promised": "^7.1.1", "eslint": "^8.1.0", "eslint-config-hexo": "^5.0.0", diff --git a/test/scripts/database.js b/test/scripts/database.js index 30001e87..745165f9 100644 --- a/test/scripts/database.js +++ b/test/scripts/database.js @@ -1,17 +1,20 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line -const path = require('path'); -const Promise = require('bluebird'); -const sinon = require('sinon'); -const fs = Promise.promisifyAll(require('fs')); - +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import path from 'path'; +import Promise from 'bluebird'; +import sinon from 'sinon'; +import Database from '../../lib/database.js'; +import Model from '../../lib/model.js'; +import fs from 'fs'; +const promisifyFs = Promise.promisifyAll(fs); + +import url from "url"; +const __filename = url.fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); const DB_PATH = path.join(path.dirname(__dirname), 'fixtures', 'db.json'); const DB_VERSION = 1; describe('Database', () => { - const Database = require('../..'); - const Model = require('../../lib/model'); const Schema = Database.Schema; const db = new Database({path: DB_PATH, version: DB_VERSION}); @@ -85,7 +88,7 @@ describe('Database', () => { }); }); - it('save()', () => db.save().then(() => fs.readFileAsync(DB_PATH, 'utf8')).then(data => { + it('save()', () => db.save().then(() => promisifyFs.readFileAsync(DB_PATH, 'utf8')).then(data => { const json = JSON.parse(data); json.meta.should.eql({ diff --git a/test/scripts/document.js b/test/scripts/document.js index ba9d359c..a3a63820 100644 --- a/test/scripts/document.js +++ b/test/scripts/document.js @@ -1,10 +1,10 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import Database from '../../lib/database.js'; +import Document from '../../lib/document.js'; describe('Document', () => { - const Database = require('../..'); - const Document = require('../../lib/document'); + const db = new Database(); const Schema = Database.Schema; diff --git a/test/scripts/model.js b/test/scripts/model.js index 075dacf3..82c69070 100644 --- a/test/scripts/model.js +++ b/test/scripts/model.js @@ -1,13 +1,14 @@ -'use strict'; - -const should = require('chai').use(require('chai-as-promised')).should(); -const sortBy = require('lodash/sortBy'); -const Promise = require('bluebird'); -const sinon = require('sinon'); -const cuid = require('cuid'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import lodash from 'lodash'; +const { sortBy } = lodash; +import Promise from 'bluebird'; +import sinon from 'sinon'; +import cuid from 'cuid'; +import Database from '../../lib/database.js'; describe('Model', () => { - const Database = require('../..'); + const Schema = Database.Schema; const SchemaType = Database.SchemaType; diff --git a/test/scripts/mutex.js b/test/scripts/mutex.js index 4df3c5af..22311c56 100644 --- a/test/scripts/mutex.js +++ b/test/scripts/mutex.js @@ -1,8 +1,7 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line -const Mutex = require('../../lib/mutex'); -const sinon = require('sinon'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import Mutex from '../../lib/mutex.js'; +import sinon from 'sinon'; describe('Mutex', () => { it('mutex test', callback => { diff --git a/test/scripts/query.js b/test/scripts/query.js index 06a99af6..73d3c016 100644 --- a/test/scripts/query.js +++ b/test/scripts/query.js @@ -1,12 +1,13 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line -const sortBy = require('lodash/sortBy'); -const Promise = require('bluebird'); -const Document = require('../../lib/document'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import lodash from 'lodash'; +const { sortBy } = lodash; +import Promise from 'bluebird'; +import Document from '../../lib/document.js'; +import Database from '../../lib/database.js'; describe('Query', () => { - const Database = require('../..'); + const db = new Database(); const Schema = Database.Schema; diff --git a/test/scripts/schema.js b/test/scripts/schema.js index 6d3081b5..b0b7e3ae 100644 --- a/test/scripts/schema.js +++ b/test/scripts/schema.js @@ -1,9 +1,8 @@ -'use strict'; - -require('chai').should(); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import Database from '../../lib/database.js'; describe('Schema', () => { - const Database = require('../..'); const Schema = Database.Schema; it('add()', () => { diff --git a/test/scripts/schematype.js b/test/scripts/schematype.js index 6ed1bf25..4d5bda5b 100644 --- a/test/scripts/schematype.js +++ b/test/scripts/schematype.js @@ -1,10 +1,9 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line -const ValidationError = require('../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../lib/error/validation.js'; +import SchemaType from '../../lib/schematype.js'; describe('SchemaType', () => { - const SchemaType = require('../../lib/schematype'); const type = new SchemaType('test'); it('cast()', () => { diff --git a/test/scripts/types/array.js b/test/scripts/types/array.js index 7228343d..8831525e 100644 --- a/test/scripts/types/array.js +++ b/test/scripts/types/array.js @@ -1,13 +1,13 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line -const ValidationError = require('../../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../../lib/error/validation.js'; +import SchemaTypeArray from '../../../lib/types/array.js'; +import SchemaTypeString from '../../../lib/types/string.js'; +import SchemaTypeDate from '../../../lib/types/date.js'; +import SchemaTypeBoolean from '../../../lib/types/boolean.js'; describe('SchemaTypeArray', () => { - const SchemaTypeArray = require('../../../lib/types/array'); - const SchemaTypeString = require('../../../lib/types/string'); - const SchemaTypeDate = require('../../../lib/types/date'); - const SchemaTypeBoolean = require('../../../lib/types/boolean'); + const type = new SchemaTypeArray('test'); it('cast()', () => { diff --git a/test/scripts/types/boolean.js b/test/scripts/types/boolean.js index 07be269f..4e09bfc7 100644 --- a/test/scripts/types/boolean.js +++ b/test/scripts/types/boolean.js @@ -1,10 +1,9 @@ -'use strict'; - -require('chai').should(); -const ValidationError = require('../../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../../lib/error/validation.js'; +import SchemaTypeBoolean from '../../../lib/types/boolean.js'; describe('SchemaTypeBoolean', () => { - const SchemaTypeBoolean = require('../../../lib/types/boolean'); const type = new SchemaTypeBoolean('test'); it('cast()', () => { diff --git a/test/scripts/types/buffer.js b/test/scripts/types/buffer.js index 3c6c62d9..56bfb9a1 100644 --- a/test/scripts/types/buffer.js +++ b/test/scripts/types/buffer.js @@ -1,10 +1,9 @@ -'use strict'; - -const should = require('chai').should(); -const ValidationError = require('../../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../../lib/error/validation.js'; +import SchemaTypeBuffer from '../../../lib/types/buffer.js'; describe('SchemaTypeBuffer', () => { - const SchemaTypeBuffer = require('../../../lib/types/buffer'); const type = new SchemaTypeBuffer('test'); it('cast()', () => { diff --git a/test/scripts/types/cuid.js b/test/scripts/types/cuid.js index f17cd89c..503631cc 100644 --- a/test/scripts/types/cuid.js +++ b/test/scripts/types/cuid.js @@ -1,10 +1,9 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line -const ValidationError = require('../../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../../lib/error/validation.js'; +import SchemaTypeCUID from '../../../lib/types/cuid.js'; describe('SchemaTypeCUID', () => { - const SchemaTypeCUID = require('../../../lib/types/cuid'); const type = new SchemaTypeCUID('test'); it('cast()', () => { diff --git a/test/scripts/types/date.js b/test/scripts/types/date.js index 912bec84..15941e98 100644 --- a/test/scripts/types/date.js +++ b/test/scripts/types/date.js @@ -1,10 +1,9 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line -const ValidationError = require('../../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../../lib/error/validation.js'; +import SchemaTypeDate from '../../../lib/types/date.js'; describe('SchemaTypeDate', () => { - const SchemaTypeDate = require('../../../lib/types/date'); const type = new SchemaTypeDate('test'); it('cast()', () => { diff --git a/test/scripts/types/enum.js b/test/scripts/types/enum.js index 166c3888..47e8e8ef 100644 --- a/test/scripts/types/enum.js +++ b/test/scripts/types/enum.js @@ -1,11 +1,9 @@ -'use strict'; - -require('chai').should(); -const ValidationError = require('../../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../../lib/error/validation.js'; +import SchemaTypeEnum from '../../../lib/types/enum.js'; describe('SchemaTypeEnum', () => { - const SchemaTypeEnum = require('../../../lib/types/enum'); - it('validate()', () => { const type = new SchemaTypeEnum('test', {elements: ['foo', 'bar', 'baz']}); diff --git a/test/scripts/types/integer.js b/test/scripts/types/integer.js index 9589fad1..5216dcdb 100644 --- a/test/scripts/types/integer.js +++ b/test/scripts/types/integer.js @@ -1,10 +1,9 @@ -'use strict'; - -require('chai').should(); -const ValidationError = require('../../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../../lib/error/validation.js'; +import SchemaTypeInteger from '../../../lib/types/integer.js'; describe('SchemaTypeInteger', () => { - const SchemaTypeInteger = require('../../../lib/types/integer'); const type = new SchemaTypeInteger('test'); it('cast()', () => { diff --git a/test/scripts/types/number.js b/test/scripts/types/number.js index 6442196e..ddffd12b 100644 --- a/test/scripts/types/number.js +++ b/test/scripts/types/number.js @@ -1,10 +1,9 @@ -'use strict'; - -require('chai').should(); -const ValidationError = require('../../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../../lib/error/validation.js'; +import SchemaTypeNumber from '../../../lib/types/number.js'; describe('SchemaTypeNumber', () => { - const SchemaTypeNumber = require('../../../lib/types/number'); const type = new SchemaTypeNumber('type'); it('cast()', () => { diff --git a/test/scripts/types/object.js b/test/scripts/types/object.js index a5cd9c74..1df6445f 100644 --- a/test/scripts/types/object.js +++ b/test/scripts/types/object.js @@ -1,9 +1,8 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import SchemaTypeObject from '../../../lib/types/object.js'; describe('SchemaTypeObject', () => { - const SchemaTypeObject = require('../../../lib/types/object'); const type = new SchemaTypeObject('test'); it('cast() - default', () => { diff --git a/test/scripts/types/string.js b/test/scripts/types/string.js index 2425d012..6e4ec31f 100644 --- a/test/scripts/types/string.js +++ b/test/scripts/types/string.js @@ -1,10 +1,9 @@ -'use strict'; - -require('chai').should(); -const ValidationError = require('../../../lib/error/validation'); +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import ValidationError from '../../../lib/error/validation.js'; +import SchemaTypeString from '../../../lib/types/string.js'; describe('SchemaTypeString', () => { - const SchemaTypeString = require('../../../lib/types/string'); const type = new SchemaTypeString('test'); it('cast()', () => { diff --git a/test/scripts/types/virtual.js b/test/scripts/types/virtual.js index 515cbbcb..2a186588 100644 --- a/test/scripts/types/virtual.js +++ b/test/scripts/types/virtual.js @@ -1,9 +1,8 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import SchemaTypeVirtual from '../../../lib/types/virtual.js'; describe('SchemaTypeVirtual', () => { - const SchemaTypeVirtual = require('../../../lib/types/virtual'); const type = new SchemaTypeVirtual('test'); it('get()', () => { diff --git a/test/scripts/util.js b/test/scripts/util.js index 5a23df2a..871a52ef 100644 --- a/test/scripts/util.js +++ b/test/scripts/util.js @@ -1,10 +1,8 @@ -'use strict'; - -const should = require('chai').should(); // eslint-disable-line +import chai from 'chai'; +const should = chai.should(); // eslint-disable-line +import * as util from '../../lib/util.js'; describe('util', () => { - const util = require('../../lib/util'); - it('shuffle()', () => { const src = Array(100).fill(0).map((_, i) => i); const result = util.shuffle(src);