Skip to content

u: supports asynchronous callback #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions JsonExcel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default {
return;
}

let json = this.getProcessedJson(data, this.downloadFields);
let json = await this.getProcessedJson(data, this.downloadFields);
if (this.type === "html") {
// this is mainly for testing
return this.export(
Expand Down Expand Up @@ -247,18 +247,21 @@ export default {
---------------
Get only the data to export, if no fields are set return all the data
*/
getProcessedJson(data, header) {
async getProcessedJson(data, header) {
let keys = this.getKeys(data, header);
let newData = [];
let _self = this;
data.map(function (item, index) {
await data.reduce(async function (prev, current) {
await prev;
let newItem = {};
for (let label in keys) {
let property = keys[label];
newItem[label] = _self.getValue(property, item);
newItem[label] = await _self.getValue(property, current);
}
newData.push(newItem);
});

return true;
}, []);

return newData;
},
Expand Down Expand Up @@ -291,7 +294,7 @@ export default {
return parseData;
},

getValue(key, item) {
async getValue(key, item) {
const field = typeof key !== "object" ? key : key.field;
let indexes = typeof field !== "string" ? [] : field.split(".");
let value = this.defaultValue;
Expand All @@ -302,7 +305,7 @@ export default {
else value = this.parseValue(item[field]);

if (key.hasOwnProperty("callback"))
value = this.getValueFromCallback(value, key.callback);
value = await this.getValueFromCallback(value, key.callback);

return value;
},
Expand Down Expand Up @@ -335,9 +338,9 @@ export default {
return this.parseValue(nestedItem);
},

getValueFromCallback(item, callback) {
async getValueFromCallback(item, callback) {
if (typeof callback !== "function") return this.defaultValue;
const value = callback(item);
const value = await callback(item);
return this.parseValue(value);
},
parseValue(value) {
Expand Down
27 changes: 17 additions & 10 deletions dist/vue-json-excel.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ var script = {
return;
}

let json = this.getProcessedJson(data, this.downloadFields);
let json = await this.getProcessedJson(data, this.downloadFields);
if (this.type === "html") {
// this is mainly for testing
return this.export(
Expand Down Expand Up @@ -412,18 +412,21 @@ var script = {
---------------
Get only the data to export, if no fields are set return all the data
*/
getProcessedJson(data, header) {
async getProcessedJson(data, header) {
let keys = this.getKeys(data, header);
let newData = [];
let _self = this;
data.map(function (item, index) {
await data.reduce(async function (prev, current) {
await prev;
let newItem = {};
for (let label in keys) {
let property = keys[label];
newItem[label] = _self.getValue(property, item);
newItem[label] = await _self.getValue(property, current);
}
newData.push(newItem);
});

return true;
}, []);

return newData;
},
Expand Down Expand Up @@ -456,7 +459,7 @@ var script = {
return parseData;
},

getValue(key, item) {
async getValue(key, item) {
const field = typeof key !== "object" ? key : key.field;
let indexes = typeof field !== "string" ? [] : field.split(".");
let value = this.defaultValue;
Expand All @@ -467,7 +470,7 @@ var script = {
else value = this.parseValue(item[field]);

if (key.hasOwnProperty("callback"))
value = this.getValueFromCallback(value, key.callback);
value = await this.getValueFromCallback(value, key.callback);

return value;
},
Expand Down Expand Up @@ -500,9 +503,9 @@ var script = {
return this.parseValue(nestedItem);
},

getValueFromCallback(item, callback) {
async getValueFromCallback(item, callback) {
if (typeof callback !== "function") return this.defaultValue;
const value = callback(item);
const value = await callback(item);
return this.parseValue(value);
},
parseValue(value) {
Expand Down Expand Up @@ -619,7 +622,11 @@ var __vue_render__ = function() {
return _c(
"div",
{ attrs: { id: _vm.idName }, on: { click: _vm.generate } },
[_vm._t("default", [_vm._v(" Download " + _vm._s(_vm.name) + " ")])],
[
_vm._t("default", function() {
return [_vm._v(" Download " + _vm._s(_vm.name) + " ")]
})
],
2
)
};
Expand Down
27 changes: 17 additions & 10 deletions dist/vue-json-excel.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ var script = {
return;
}

let json = this.getProcessedJson(data, this.downloadFields);
let json = await this.getProcessedJson(data, this.downloadFields);
if (this.type === "html") {
// this is mainly for testing
return this.export(
Expand Down Expand Up @@ -410,18 +410,21 @@ var script = {
---------------
Get only the data to export, if no fields are set return all the data
*/
getProcessedJson(data, header) {
async getProcessedJson(data, header) {
let keys = this.getKeys(data, header);
let newData = [];
let _self = this;
data.map(function (item, index) {
await data.reduce(async function (prev, current) {
await prev;
let newItem = {};
for (let label in keys) {
let property = keys[label];
newItem[label] = _self.getValue(property, item);
newItem[label] = await _self.getValue(property, current);
}
newData.push(newItem);
});

return true;
}, []);

return newData;
},
Expand Down Expand Up @@ -454,7 +457,7 @@ var script = {
return parseData;
},

getValue(key, item) {
async getValue(key, item) {
const field = typeof key !== "object" ? key : key.field;
let indexes = typeof field !== "string" ? [] : field.split(".");
let value = this.defaultValue;
Expand All @@ -465,7 +468,7 @@ var script = {
else value = this.parseValue(item[field]);

if (key.hasOwnProperty("callback"))
value = this.getValueFromCallback(value, key.callback);
value = await this.getValueFromCallback(value, key.callback);

return value;
},
Expand Down Expand Up @@ -498,9 +501,9 @@ var script = {
return this.parseValue(nestedItem);
},

getValueFromCallback(item, callback) {
async getValueFromCallback(item, callback) {
if (typeof callback !== "function") return this.defaultValue;
const value = callback(item);
const value = await callback(item);
return this.parseValue(value);
},
parseValue(value) {
Expand Down Expand Up @@ -617,7 +620,11 @@ var __vue_render__ = function() {
return _c(
"div",
{ attrs: { id: _vm.idName }, on: { click: _vm.generate } },
[_vm._t("default", [_vm._v(" Download " + _vm._s(_vm.name) + " ")])],
[
_vm._t("default", function() {
return [_vm._v(" Download " + _vm._s(_vm.name) + " ")]
})
],
2
)
};
Expand Down
27 changes: 17 additions & 10 deletions dist/vue-json-excel.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
return;
}

let json = this.getProcessedJson(data, this.downloadFields);
let json = await this.getProcessedJson(data, this.downloadFields);
if (this.type === "html") {
// this is mainly for testing
return this.export(
Expand Down Expand Up @@ -416,18 +416,21 @@
---------------
Get only the data to export, if no fields are set return all the data
*/
getProcessedJson(data, header) {
async getProcessedJson(data, header) {
let keys = this.getKeys(data, header);
let newData = [];
let _self = this;
data.map(function (item, index) {
await data.reduce(async function (prev, current) {
await prev;
let newItem = {};
for (let label in keys) {
let property = keys[label];
newItem[label] = _self.getValue(property, item);
newItem[label] = await _self.getValue(property, current);
}
newData.push(newItem);
});

return true;
}, []);

return newData;
},
Expand Down Expand Up @@ -460,7 +463,7 @@
return parseData;
},

getValue(key, item) {
async getValue(key, item) {
const field = typeof key !== "object" ? key : key.field;
let indexes = typeof field !== "string" ? [] : field.split(".");
let value = this.defaultValue;
Expand All @@ -471,7 +474,7 @@
else value = this.parseValue(item[field]);

if (key.hasOwnProperty("callback"))
value = this.getValueFromCallback(value, key.callback);
value = await this.getValueFromCallback(value, key.callback);

return value;
},
Expand Down Expand Up @@ -504,9 +507,9 @@
return this.parseValue(nestedItem);
},

getValueFromCallback(item, callback) {
async getValueFromCallback(item, callback) {
if (typeof callback !== "function") return this.defaultValue;
const value = callback(item);
const value = await callback(item);
return this.parseValue(value);
},
parseValue(value) {
Expand Down Expand Up @@ -623,7 +626,11 @@
return _c(
"div",
{ attrs: { id: _vm.idName }, on: { click: _vm.generate } },
[_vm._t("default", [_vm._v(" Download " + _vm._s(_vm.name) + " ")])],
[
_vm._t("default", function() {
return [_vm._v(" Download " + _vm._s(_vm.name) + " ")]
})
],
2
)
};
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-json-excel",
"version": "0.3.0",
"version": "0.4.0",
"description": "Download your JSON as an excel or CSV file directly from the browser",
"main": "dist/vue-json-excel.umd.js",
"module": "dist/vue-json-excel.esm.js",
Expand Down Expand Up @@ -41,4 +41,4 @@
"rollup-plugin-vue": "^4.7.2",
"vue-template-compiler": "^2.6.10"
}
}
}