Skip to content

Commit

Permalink
feat(Exists): Add exits method
Browse files Browse the repository at this point in the history
To check the existence of a DataPath.

See #19
  • Loading branch information
Antoine Aflalo committed Sep 4, 2018
1 parent 6b92860 commit 35152a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/JsonDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ export default class JsonDB {
return this.retrieveData(path, false)
}

/**
* Check for existing datapath
* @param dataPath
*/
public exists(dataPath: string): boolean {
try {
this.getData(dataPath)
return true
} catch (e) {
if (e instanceof DataError) {
return false
}
throw e
}
}

/**
* Pushing data into the database
* @param dataPath path leading to the data
Expand Down
7 changes: 7 additions & 0 deletions test/02-jsondb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ describe('JsonDB', () => {
db.push("/", object)
expect(db.getData("/")).toBe(object)
})

test('should have data at root', () => {
expect(db.exists('/test/test')).toBeTruthy()
})
test('should not have data at not related path', () => {
expect(db.exists('/test/test/nope')).toBeFalsy()
})
test('should override the data at the root', () => {
const object = {test: "test"}
db.push("/", object)
Expand Down

0 comments on commit 35152a2

Please sign in to comment.