Skip to content

Commit

Permalink
feat(Filter): Add filtering feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Belphemur committed Feb 20, 2019
1 parent 9ba7fbe commit 0f7d276
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/JsonDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,24 @@ export default class JsonDB {
* @param rootPath base dataPath from where to start searching
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
*/
public findAll<T>(rootPath: string, callback: FindCallback): T | undefined {
public filter<T>(rootPath: string, callback: FindCallback): T[] | undefined {
const result = this.getData(rootPath)
if (Array.isArray(result)) {
return result.find(callback) as T
return result.filter(callback) as T[]
}
if (result instanceof Object) {
const entries = Object.entries(result)
const found = entries.find((entry: Array<any>) => {
const found = entries.filter((entry: [string, any]) => {
return callback(entry[1], entry[0])
})
if (!found || found.length < 2) {
}) as [string, T][]

if (!found || found.length < 1) {
return undefined
}
return found

return found.map((entry: [string, T]) => {
return entry[1]
})
}
throw new DataError("The entry at the path (" + rootPath + ") needs to be either an Object or an Array", 12)
}
Expand All @@ -185,10 +189,21 @@ export default class JsonDB {
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
*/
public find<T>(rootPath: string, callback: FindCallback): T | undefined {
found = this.findAll(rootPath, callback)
if(found === undefined)
return undefined
return found[1]
const result = this.getData(rootPath)
if (Array.isArray(result)) {
return result.find(callback) as T
}
if (result instanceof Object) {
const entries = Object.entries(result)
const found = entries.find((entry: Array<any>) => {
return callback(entry[1], entry[0])
})
if (!found || found.length < 2) {
return undefined
}
return found[1] as T
}
throw new DataError("The entry at the path (" + rootPath + ") needs to be either an Object or an Array", 12)
}

/**
Expand Down
31 changes: 31 additions & 0 deletions test/02-jsondb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,37 @@ describe('JsonDB', () => {
})
})

describe('Filter Info', () => {
const db = new JsonDB(testFile6, true)

test('should be able to filter object matching filter',
() => {
db.push('/filter/id-0', {test: 'hello'})
db.push('/filter/id-1', {test: 'hey'})
db.push('/filter/id-2', {test: 'echo'})
db.push('/filter/id-3', {test: 'hello'})
const result = db.filter<{test: string}>('/filter', entry => entry.test === 'hello')
expect(result).toBeInstanceOf(Array)
expect(result).toHaveLength(2)
expect(result![0]).toHaveProperty('test', 'hello')
expect(result![1]).toHaveProperty('test', 'hello')
})
test('should be able to filter the array matching filter',
() => {
db.push('/filter/data', [{test: 'echo'}, {test: 'hey'}, {test: 'hello'}, {test: 'echo'}])
const result = db.filter<{test: string}>('/filter/data', entry => entry.test === 'echo')
expect(result).toBeInstanceOf(Array)
expect(result).toHaveLength(2)
expect(result![0]).toHaveProperty('test', 'echo')
expect(result![1]).toHaveProperty('test', 'echo')
})
test('shouldn\'t be able to find a data in anything else than Object or Array',
() => {
db.push('/filter/number', 1)
expect(() => db.find<{test: string}>('/filter/number', entry => entry.test === 'hello')).toThrow(DataError)
})
})

describe('Cleanup', () => {
test('should remove the test files', () => {
fs.unlinkSync(testFile1 + ".json")
Expand Down

0 comments on commit 0f7d276

Please sign in to comment.