-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added find, filter and error messages.
- Loading branch information
1 parent
99cbc93
commit c128d0f
Showing
10 changed files
with
100 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const fs = require('fs'); | ||
|
||
function statPath(path) { | ||
try { | ||
return fs.statSync(path); | ||
} catch (ex) { } | ||
return false; | ||
} | ||
|
||
function dataControl() { | ||
var exist = statPath('./data/db.json'); | ||
if (exist && exist.isFile()) { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
module.exports = dataControl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const getAllData = require("./getAllData") | ||
|
||
function dataFilter(arrayName, condition) { | ||
const dataAll = getAllData(); | ||
return dataAll[arrayName].filter(condition) | ||
} | ||
|
||
module.exports = dataFilter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const getAllData = require("./getAllData") | ||
|
||
function dataFind(arrayName, condition) { | ||
const dataAll = getAllData(); | ||
return dataAll[arrayName].find(condition) | ||
} | ||
|
||
module.exports = dataFind; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const log = require('../utils/log'); | ||
const dataControl = require('./dataControl'); | ||
|
||
function init() { | ||
fs.mkdir('data', function (error) { | ||
if (error) console.log(error); | ||
console.log('Data folder created.'); | ||
}); | ||
if (!dataControl()) { | ||
fs.mkdir('data', function (error) { | ||
console.log('Data folder created.'); | ||
}); | ||
|
||
fs.writeFile('data/db.json', `{ | ||
"products": [ | ||
{ | ||
"id": 1, | ||
"categoryId": 2, | ||
"productName": "Chai", | ||
"quantityPerUnit": "48 - 6 oz jars", | ||
"unitPrice": "30", | ||
"unitsInStock": 53 | ||
} | ||
] | ||
}`, function (error) { | ||
if (error) console.log(error); | ||
console.log("db.json created."); | ||
}) | ||
} | ||
fs.writeFile('data/db.json', `{ | ||
"products": [ | ||
{ | ||
"id": 1, | ||
"categoryId": 2, | ||
"productName": "Chai", | ||
"quantityPerUnit": "48 - 6 oz jars", | ||
"unitPrice": "30", | ||
"unitsInStock": 53 | ||
} | ||
] | ||
}`, function (error) { | ||
if (error) console.log(error); | ||
console.log("db.json created."); | ||
}) | ||
} else { | ||
log("Data already exists", "Error", "error") | ||
} | ||
|
||
} | ||
module.exports = init; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const dataFilter = require('../helpers/dataFilter'); | ||
|
||
function filter(arrayName, condition) { | ||
return dataFilter(arrayName, condition); | ||
} | ||
|
||
module.exports = filter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const dataFind = require('../helpers/dataFind'); | ||
|
||
function find(arrayName, condition) { | ||
return dataFind(arrayName, condition); | ||
} | ||
|
||
module.exports = find; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
const alert = require('cli-alerts'); | ||
|
||
module.exports = info => { | ||
module.exports = (msg, title, type, info) => { | ||
alert({ | ||
type: `warning`, | ||
name: `DEBUG LOG`, | ||
msg: `` | ||
type: type, | ||
name: title, | ||
msg: msg | ||
}); | ||
|
||
console.log(info); | ||
console.log(); | ||
if (info) console.log(info); | ||
}; |