diff --git a/demo/index.js b/demo/index.js deleted file mode 100644 index eb7b790..0000000 --- a/demo/index.js +++ /dev/null @@ -1,66 +0,0 @@ -import { Queryable } from '../dist/index.js'; - -// let promiseApi = new Promise((resolve, reject) => { -// // skills: attack, stamia, speed, shoot -// console.log('get players...'); -// setTimeout(() => { -// resolve([ -// { name: 'Ronaldo', overall: 96, nationId: 1, skills: [96, 85, 87, 91] }, -// { name: 'Messi', overall: 98, nationId: 2, skills: [97, 85, 91, 93] }, -// { name: 'Mbappe', overall: 86, nationId: 3, skills: [89, 81, 95, 83] }, -// { name: 'Matial', overall: 81, nationId: 3, skills: [81, 80, 89, 81] }, -// { name: 'Salah', overall: 89, nationId: 4, skills: [88, 82, 97, 86] } -// ]); -// }, 1000); -// }) - -let nations = new Promise(resolve => { - console.log('get nations...'); - setTimeout(() => { - resolve([ - { id: 1, name: 'Portugal', areaId: 1 }, - { id: 2, name: 'Argentina', areaId: 2 }, - { id: 3, name: 'France', areaId: 1 }, - { id: 4, name: 'Egypt', areaId: 3 } - ]); - }, 2000); -}) - -let continents = new Promise(resolve => { - console.log('get continents...'); - setTimeout(() => { - resolve([ - { id: 1, areaName: 'Euro' }, - { id: 2, areaName: 'South America' }, - ]); - }, 2300); -}) - - -function main() { - // Just query not execute query - console.time('querytime'); - let query = - Queryable.from(nations) - .join(continents, (x, y) => x.areaId === y.id) - .groupBy(o => o.y.areaName) - .select(x => { - let _tmp = {}; - return { - area: x.key, - total: Queryable.from(x.items).count() - } - }) - - - console.timeEnd('querytime'); - - console.time('executetime'); - const data = query.toList().then(data => { - console.timeEnd('executetime'); - console.log(data); - console.table(data); - }); -} - -main(); \ No newline at end of file diff --git a/package.json b/package.json index a3a10e2..f460aea 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "LINQ for Javascript, written by TypeScript", "main": "index.js", "scripts": { - "test": "mocha -r ts-node/register src/**/*.test.ts" + "test": "mocha -r ts-node/register test/*.ts" }, "repository": { "type": "git", diff --git a/src/demo/test.ts b/src/demo/test.ts deleted file mode 100644 index 3fb6a68..0000000 --- a/src/demo/test.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { Queryable } from "../implements/queryable"; -const cTable = require('console.table'); - -let players = new Promise<{ name, overall, nationId, skills }[]>((resolve, reject) => { - // skills: attack, stamia, speed, shoot - console.log('get players...'); - setTimeout(() => { - resolve([ - { name: 'Ronaldo', overall: 96, nationId: 1, skills: [96, 85, 87, 91] }, - { name: 'Messi', overall: 98, nationId: 2, skills: [97, 85, 91, 93] }, - { name: 'Mbappe', overall: 86, nationId: 3, skills: [89, 81, 95, 83] }, - { name: 'Matial', overall: 81, nationId: 3, skills: [81, 80, 89, 81] }, - { name: 'Salah', overall: 89, nationId: 4, skills: [88, 82, 97, 86] } - ]); - }, 1000); -}) - -let nations: Promise<{ id, name, areaId }[]> = new Promise(resolve => { - console.log('get nations...'); - setTimeout(() => { - resolve([ - { id: 1, name: 'Portugal', areaId: 1 }, - { id: 2, name: 'Argentina', areaId: 2 }, - { id: 3, name: 'France', areaId: 1 }, - { id: 4, name: 'Egypt', areaId: 3 } - ]); - }, 2000); -}) - -let continents = new Promise<{ id, areaName }[]>(resolve => { - console.log('get continents...'); - setTimeout(() => { - resolve([ - { id: 1, areaName: 'Euro' }, - { id: 2, areaName: 'South America' }, - ]); - }, 2300); -}) - -function main() { - let query = Queryable - .from(players) - .where(x => x.overall > 85); - - let query1 = query.clone() - .join(nations, (x, y) => x.nationId === y.id) - .select(o => { - return { - playerName: o.x.name, - nation: o.y.name - } - }); - - let query2 = query.clone() - .select(o => { - return { - name: o.name, - realOverall: Queryable.fromSync(o.skills).avarage() - } - }).orderByDescending(x => x.realOverall); - - query.toList().then(data => console.table(data)); - query1.toList().then(data => console.table(data)); - query2.toList().then(data => console.table(data)); -} - -main(); - - diff --git a/src/test/join.test.tss b/src/test/join.test.tss deleted file mode 100644 index 292625f..0000000 --- a/src/test/join.test.tss +++ /dev/null @@ -1,37 +0,0 @@ -import { promiseApi, staticAreas, staticLoopkup } from './source'; -import { expect } from 'chai'; -import { Queryable } from '../implements/queryable'; - -describe('join', () => { - async function run() { - const queryable = new Queryable<{ name, nationId, overall }>(); - let query = queryable - .from(promiseApi) - .join(staticLoopkup, (x, y) => x.nationId === y.id) - .join(staticAreas, (x, y) => x.y.areaId === y.id) - .select(item => { - return { - playerName: item.x.x.name, - nation: item.x.y.name, - area: item.y.areaName - } - }); - return { - data: await query.toList(), - originalData: await promiseApi as any[], - distinct: staticLoopkup.map(x => x.areaId).filter(function (item, pos, self) { - return self.indexOf(item) == pos; - }) - } - } - - it('items equal num of records from last array', async () => { - const rs = await run(); - expect(rs.data.length).to.equal(rs.distinct.length) - }); - - it('result data always equal or less than original', async () => { - const rs = await run(); - expect(rs.data.length).to.be.most(rs.distinct.length) - }) -}); \ No newline at end of file diff --git a/src/test/leftJoin.test.tss b/src/test/leftJoin.test.tss deleted file mode 100644 index 7d0d2d2..0000000 --- a/src/test/leftJoin.test.tss +++ /dev/null @@ -1,38 +0,0 @@ -import { promiseApi, staticAreas, staticLoopkup } from './source'; -import { expect } from 'chai'; -import { Queryable } from '../implements/queryable'; - -describe('left join', () => { - async function run() { - const queryable = new Queryable<{ name, nationId, overall }>(); - let query = queryable - .from(promiseApi) - .join(staticLoopkup, (x, y) => x.nationId === y.id) - .leftJoin(staticAreas, (x, y) => x.y.areaId === y.id) - .select(item => { - return { - name: item.x.name, - area: item.areaName - } - }); - return { - data: await query.toList(), - originalData: await promiseApi as any[] - } - } - - it('keep num of items', async () => { - const rs = await run(); - expect(rs.data.length).to.equal(rs.originalData.length) - }); - - it('last one didn\'t join', async () => { - const rs = await run(); - expect(rs.data[rs.data.length - 1].area).to.equal(undefined); - }) - - it('first one get exists data', async () => { - const rs = await run(); - expect(rs.data[0].area).not.equal(undefined); - }) -}); \ No newline at end of file diff --git a/src/test/selectMany.test.tss b/src/test/selectMany.test.tss deleted file mode 100644 index a236369..0000000 --- a/src/test/selectMany.test.tss +++ /dev/null @@ -1,27 +0,0 @@ -import { promiseApi, staticAreas, staticLoopkup } from './source'; -import { expect } from 'chai'; -import { Queryable } from '../implements/queryable'; - -describe('selectMany', () => { - async function run() { - const queryable = new Queryable<{ name, nationId, overall, skills }>(); - let query = queryable - .from(promiseApi) - .selectMany(x => x.skills); - - return { - data: await query.toList(), - originalData: await promiseApi as any[], - distinct: staticLoopkup.map(x => x.areaId).filter(function (item, pos, self) { - return self.indexOf(item) == pos; - }) - } - } - - it('is fatten list (this case is number)', async () => { - const rs = await run(); - expect(rs.data).to.satisfy((item) => { - return rs.data.every((num) => Number.isInteger(num as number)); - }); - }); -}); \ No newline at end of file diff --git a/src/test/source.ts b/src/test/source.ts deleted file mode 100644 index eebcbea..0000000 --- a/src/test/source.ts +++ /dev/null @@ -1,26 +0,0 @@ -export let promiseApi = new Promise((resolve, reject) => { - // Fake data from api - setTimeout(() => { - console.log('...get data'); - resolve([ - { name: 'Ronaldo', overall: 96, nationId: 1, skills: [97, 90, 86, 95] }, - { name: 'Messi', overall: 98, nationId: 2, skills: [97, 90, 86, 95] }, - { name: 'Mbappe', overall: 86, nationId: 3, skills: [97, 90, 86, 95] }, - { name: 'Salah', overall: 89, nationId: 4, skills: [97, 90, 86, 95] } - ]); - }, 1000); -}) - -export let staticLoopkup = [ - { id: 1, name: 'Portugal', areaId: 1 }, - { id: 2, name: 'Argentina', areaId: 2 }, - { id: 3, name: 'France', areaId: 1 }, - { id: 4, name: 'Egypt', areaId: 3 } -] - -export let staticAreas = [ - { id: 1, areaName: 'Euro' }, - { id: 2, areaName: 'South America' }, -] - - diff --git a/test/join.ts b/test/join.ts new file mode 100644 index 0000000..31d4ed3 --- /dev/null +++ b/test/join.ts @@ -0,0 +1,29 @@ +import { Queryable } from "../dist"; +import { nations, players, continents } from "./source"; +import { expect } from "chai"; + +describe('join', () => { + const query = Queryable.from(players) + .join(nations, (x, y) => x.nationId === y.id) + + it('can run', () => { + query.toList().then(data => { + expect(data).not.equal(undefined); + }); + }); + + it('put out {x,y} object have value', () => { + query.toList().then(data => { + expect(data[0].x).not.equal(undefined); + expect(data[0].y).not.equal(undefined); + expect(data[0].y).not.equal(null); + expect(data[0].y).not.equal(null); + }); + }); + + it('cannot over original length', () => { + Promise.all([query.toList(), players]).then((arr: any[]) => { + expect(arr[0].length).not.greaterThan(arr[1].length); + }); + }) +}); \ No newline at end of file diff --git a/test/leftJoin.ts b/test/leftJoin.ts new file mode 100644 index 0000000..e866437 --- /dev/null +++ b/test/leftJoin.ts @@ -0,0 +1,30 @@ +import { Queryable } from "../dist"; +import { nations, players, continents } from "./source"; +import { expect } from "chai"; + +describe('leftJoin', () => { + const query = Queryable.from(players) + .leftJoin(nations, (x, y) => x.nationId === y.id); + + it('can run', () => { + query.toList().then(data => { + expect(data).not.equal(undefined); + }); + }); + + it('all properties in new object is inside input object properties', () => { + Promise.all([query.toList(), players, nations]).then((arr: any[]) => { + const arr0Keys = Object.keys(arr[0][0]); + const arr1Keys = Object.keys(arr[1][0]); + const arr2Keys = Object.keys(arr[2][0]); + + arr1Keys.forEach(p => { + expect(arr0Keys.includes(p)).to.equal(true); + }) + + arr2Keys.forEach(p => { + expect(arr0Keys.includes(p)).to.equal(true); + }) + }); + }) +}); \ No newline at end of file diff --git a/test/selectMany.ts b/test/selectMany.ts new file mode 100644 index 0000000..b931c05 --- /dev/null +++ b/test/selectMany.ts @@ -0,0 +1,15 @@ +import { players, continents, nations } from './source'; +import { expect } from 'chai'; +import { Queryable } from '../dist'; + +describe('selectMany', () => { + const query = Queryable.from(players) + .selectMany(x => x.skills); + + it('is fatten list (this case is number)', async () => { + const data: any[] = await query.toList(); + data.forEach(item => { + expect(Number.isInteger(item)).to.equal(true) + }); + }) +}); \ No newline at end of file diff --git a/test/source.ts b/test/source.ts new file mode 100644 index 0000000..db0d7ce --- /dev/null +++ b/test/source.ts @@ -0,0 +1,32 @@ +export let players = new Promise<{ name, overall, nationId, skills }[]>((resolve, reject) => { + // skills: attack, stamia, speed, shoot + setTimeout(() => { + resolve([ + { name: 'Ronaldo', overall: 96, nationId: 1, skills: [96, 85, 87, 91] }, + { name: 'Messi', overall: 98, nationId: 2, skills: [97, 85, 91, 93] }, + { name: 'Mbappe', overall: 86, nationId: 3, skills: [89, 81, 95, 83] }, + { name: 'Matial', overall: 81, nationId: 3, skills: [81, 80, 89, 81] }, + { name: 'Salah', overall: 89, nationId: 4, skills: [88, 82, 97, 86] } + ]); + }, 1000); +}) + +export let nations = new Promise<{ id, name, areaId }[]>(resolve => { + setTimeout(() => { + resolve([ + { id: 1, name: 'Portugal', areaId: 1 }, + { id: 2, name: 'Argentina', areaId: 2 }, + { id: 3, name: 'France', areaId: 1 }, + { id: 4, name: 'Egypt', areaId: 3 } + ]); + }, 2000); +}) + +export let continents = new Promise<{ id, areaName }[]>(resolve => { + setTimeout(() => { + resolve([ + { id: 1, areaName: 'Euro' }, + { id: 2, areaName: 'South America' }, + ]); + }, 2300); +}) \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 2d2827c..93ca402 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "importHelpers": true, "sourceMap": true, "outDir": "./dist", + "rootDir": "src", "moduleResolution": "node", // "module": "amd", "emitDecoratorMetadata": true, @@ -15,6 +16,17 @@ "lib": [ "es2017", "dom" - ] - } + ], + "paths": { + "tslib": [ + "tslib" + ] + }, + "baseUrl": "./", + "declaration": true, + }, + "exclude": [ + "demo", + "test" + ] } \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index 68fd456..27688e5 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -3,7 +3,7 @@ const HTMLWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: { - index: './demo/index.js', + index: './demo/webpack/index.js', }, plugins: [ new HTMLWebpackPlugin({ @@ -12,6 +12,6 @@ module.exports = { ], output: { filename: '[name].bundle.js', - path: path.resolve(__dirname, 'dist') + path: path.resolve(__dirname, 'dist/browser-test') } }; \ No newline at end of file