parse-googlesheets
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -0,11 +1,14 @@ | ||
export declare const string: (data: string | undefined) => string | undefined; | ||
export declare const number: (data: string | undefined) => number | undefined; | ||
export declare const parseGoogleSheets: <T extends Schema>(schema: T, data: SheetsResponse) => Row<T>[]; | ||
export declare const loadGoogleSheets: <T extends Schema>(schema: T, SheetId: string, SheetListNumber?: number) => Promise<Row<T>[]>; | ||
declare type FieldData = { | ||
type: 'string' | 'number'; | ||
parse: Func<[string | undefined], any>; | ||
columnName: string; | ||
}; | ||
declare type Schema = { | ||
export declare type Schema = { | ||
[Key: string]: FieldData; | ||
}; | ||
declare type Row<T extends Schema> = { | ||
[Key in keyof T]?: T[Key]['type'] extends 'string' ? string : number; | ||
export declare type Row<T extends Schema> = { | ||
[Key in keyof T]: ReturnType<T[Key]['parse']>; | ||
}; | ||
@@ -25,2 +28,3 @@ declare type SheetsResponse = { | ||
}; | ||
declare type Func<Arguments extends Array<any>, Return> = (...args: Arguments) => Return; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseGoogleSheets = void 0; | ||
exports.loadGoogleSheets = exports.parseGoogleSheets = exports.number = exports.string = void 0; | ||
var request_1 = require("./request"); | ||
var ifElse = function (expression, onTrue, onFalse) { | ||
return function (data) { return expression(data) | ||
? onTrue(data) | ||
: onFalse(data); }; | ||
}; | ||
var isNil = function (data) { | ||
return data === null || data === undefined; | ||
}; | ||
var always = function (data) { return function () { return data; }; }; | ||
var identity = function (data) { return data; }; | ||
var toNumberOrNil = function (value) { return isNaN(Number(value)) | ||
? undefined | ||
: Number(value); }; | ||
exports.string = ifElse(isNil, always(undefined), identity); | ||
exports.number = ifElse(isNil, always(undefined), toNumberOrNil); | ||
var parseGoogleSheets = function (schema, data) { | ||
@@ -21,13 +37,11 @@ var rows = data.feed.entry.reduce(function (acc, item) { | ||
return columnName.toLowerCase() === sheetsColumnName.trim().toLowerCase(); | ||
})) !== null && _a !== void 0 ? _a : [undefined, { type: undefined }], dataName = _b[0], type = _b[1].type; | ||
if (dataName && type) { | ||
acc[dataName] = { index: index, type: type }; | ||
})) !== null && _a !== void 0 ? _a : [undefined, { type: undefined }], dataName = _b[0], parse = _b[1].parse; | ||
if (dataName && parse) { | ||
acc[dataName] = { index: index, parse: parse }; | ||
} | ||
return acc; | ||
}, Object.create(null)); | ||
var toNumber = function (value) { return isNaN(Number(value)) ? undefined : Number(value); }; | ||
return rows.slice(1).map(function (row) { return Object.entries(indexMap).reduce(function (acc, _a) { | ||
var key = _a[0], _b = _a[1], index = _b.index, type = _b.type; | ||
// @ts-ignore | ||
acc[key] = type === 'string' ? row[index] : toNumber(row[index]); | ||
var key = _a[0], _b = _a[1], index = _b.index, parse = _b.parse; | ||
acc[key] = parse(row[index]); | ||
return acc; | ||
@@ -37,1 +51,8 @@ }, Object.create(null)); }); | ||
exports.parseGoogleSheets = parseGoogleSheets; | ||
var loadGoogleSheets = function (schema, SheetId, SheetListNumber) { | ||
if (SheetListNumber === void 0) { SheetListNumber = 1; } | ||
return request_1.request("https://spreadsheets.google.com/feeds/cells/" + SheetId + "/" + SheetListNumber + "/public/full?alt=json") | ||
.then(function (r) { return r.ok ? r.json() : r.text().then(function (message) { return Promise.reject(message); }); }) | ||
.then(function (sheets) { return exports.parseGoogleSheets(schema, sheets); }); | ||
}; | ||
exports.loadGoogleSheets = loadGoogleSheets; |
@@ -19,7 +19,23 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
\**********************/ | ||
/***/ ((__unused_webpack_module, exports) => { | ||
/***/ ((__unused_webpack_module, exports, __webpack_require__) => { | ||
Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
exports.parseGoogleSheets = void 0; | ||
exports.loadGoogleSheets = exports.parseGoogleSheets = exports.number = exports.string = void 0; | ||
var request_1 = __webpack_require__(/*! ./request */ "./src/request.ts"); | ||
var ifElse = function (expression, onTrue, onFalse) { | ||
return function (data) { return expression(data) | ||
? onTrue(data) | ||
: onFalse(data); }; | ||
}; | ||
var isNil = function (data) { | ||
return data === null || data === undefined; | ||
}; | ||
var always = function (data) { return function () { return data; }; }; | ||
var identity = function (data) { return data; }; | ||
var toNumberOrNil = function (value) { return isNaN(Number(value)) | ||
? undefined | ||
: Number(value); }; | ||
exports.string = ifElse(isNil, always(undefined), identity); | ||
exports.number = ifElse(isNil, always(undefined), toNumberOrNil); | ||
var parseGoogleSheets = function (schema, data) { | ||
@@ -42,13 +58,11 @@ var rows = data.feed.entry.reduce(function (acc, item) { | ||
return columnName.toLowerCase() === sheetsColumnName.trim().toLowerCase(); | ||
})) !== null && _a !== void 0 ? _a : [undefined, { type: undefined }], dataName = _b[0], type = _b[1].type; | ||
if (dataName && type) { | ||
acc[dataName] = { index: index, type: type }; | ||
})) !== null && _a !== void 0 ? _a : [undefined, { type: undefined }], dataName = _b[0], parse = _b[1].parse; | ||
if (dataName && parse) { | ||
acc[dataName] = { index: index, parse: parse }; | ||
} | ||
return acc; | ||
}, Object.create(null)); | ||
var toNumber = function (value) { return isNaN(Number(value)) ? undefined : Number(value); }; | ||
return rows.slice(1).map(function (row) { return Object.entries(indexMap).reduce(function (acc, _a) { | ||
var key = _a[0], _b = _a[1], index = _b.index, type = _b.type; | ||
// @ts-ignore | ||
acc[key] = type === 'string' ? row[index] : toNumber(row[index]); | ||
var key = _a[0], _b = _a[1], index = _b.index, parse = _b.parse; | ||
acc[key] = parse(row[index]); | ||
return acc; | ||
@@ -58,4 +72,27 @@ }, Object.create(null)); }); | ||
exports.parseGoogleSheets = parseGoogleSheets; | ||
var loadGoogleSheets = function (schema, SheetId, SheetListNumber) { | ||
if (SheetListNumber === void 0) { SheetListNumber = 1; } | ||
return request_1.request("https://spreadsheets.google.com/feeds/cells/" + SheetId + "/" + SheetListNumber + "/public/full?alt=json") | ||
.then(function (r) { return r.ok ? r.json() : r.text().then(function (message) { return Promise.reject(message); }); }) | ||
.then(function (sheets) { return exports.parseGoogleSheets(schema, sheets); }); | ||
}; | ||
exports.loadGoogleSheets = loadGoogleSheets; | ||
/***/ }), | ||
/***/ "./src/request.ts": | ||
/*!************************!* | ||
!*** ./src/request.ts ***! | ||
\************************/ | ||
/***/ ((__unused_webpack_module, exports, __webpack_require__) => { | ||
Object.defineProperty(exports, "__esModule", ({ value: true })); | ||
exports.request = void 0; | ||
exports.request = typeof fetch === 'function' | ||
? fetch | ||
: __webpack_require__(Object(function webpackMissingModule() { var e = new Error("Cannot find module 'node-fetch'"); e.code = 'MODULE_NOT_FOUND'; throw e; }())); | ||
/***/ }) | ||
@@ -96,2 +133,2 @@ | ||
}); | ||
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly9wYXJzZS1nb29nbGVzaGVldHMvd2VicGFjay91bml2ZXJzYWxNb2R1bGVEZWZpbml0aW9uIiwid2VicGFjazovL3BhcnNlLWdvb2dsZXNoZWV0cy8uL3NyYy9pbmRleC50cyIsIndlYnBhY2s6Ly9wYXJzZS1nb29nbGVzaGVldHMvd2VicGFjay9ib290c3RyYXAiLCJ3ZWJwYWNrOi8vcGFyc2UtZ29vZ2xlc2hlZXRzL3dlYnBhY2svc3RhcnR1cCJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQSxDQUFDO0FBQ0QsTzs7Ozs7Ozs7OztBQ1ZhO0FBQ2IsOENBQTZDLENBQUMsY0FBYyxFQUFDO0FBQzdELHlCQUF5QjtBQUN6QjtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQSxLQUFLLDhCQUE4QixxQ0FBcUMsZUFBZSxFQUFFLEVBQUUsRUFBRTtBQUM3RjtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBLFNBQVMsaURBQWlELGtCQUFrQj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b2R1bGVJZCkge1xuXHQvLyBDaGVjayBpZiBtb2R1bGUgaXMgaW4gY2FjaGVcblx0aWYoX193ZWJwYWNrX21vZHVsZV9jYWNoZV9fW21vZHVsZUlkXSkge1xuXHRcdHJldHVybiBfX3dlYnBhY2tfbW9kdWxlX2NhY2hlX19bbW9kdWxlSWRdLmV4cG9ydHM7XG5cdH1cblx0Ly8gQ3JlYXRlIGEgbmV3IG1vZHVsZSAoYW5kIHB1dCBpdCBpbnRvIHRoZSBjYWNoZSlcblx0dmFyIG1vZHVsZSA9IF9fd2VicGFja19tb2R1bGVfY2FjaGVfX1ttb2R1bGVJZF0gPSB7XG5cdFx0Ly8gbm8gbW9kdWxlLmlkIG5lZWRlZFxuXHRcdC8vIG5vIG1vZHVsZS5sb2FkZWQgbmVlZGVkXG5cdFx0ZXhwb3J0czoge31cblx0fTtcblxuXHQvLyBFeGVjdXRlIHRoZSBtb2R1bGUgZnVuY3Rpb25cblx0X193ZWJwYWNrX21vZHVsZXNfX1ttb2R1bGVJZF0obW9kdWxlLCBtb2R1bGUuZXhwb3J0cywgX193ZWJwYWNrX3JlcXVpcmVfXyk7XG5cblx0Ly8gUmV0dXJuIHRoZSBleHBvcnRzIG9mIHRoZSBtb2R1bGVcblx0cmV0dXJuIG1vZHVsZS5leHBvcnRzO1xufVxuXG4iLCIvLyBtb2R1bGUgZXhwb3J0cyBtdXN0IGJlIHJldHVybmVkIGZyb20gcnVudGltZSBzbyBlbnRyeSBpbmxpbmluZyBpcyBkaXNhYmxlZFxuLy8gc3RhcnR1cFxuLy8gTG9hZCBlbnRyeSBtb2R1bGUgYW5kIHJldHVybiBleHBvcnRzXG5yZXR1cm4gX193ZWJwYWNrX3JlcXVpcmVfXyhcIi4vc3JjL2luZGV4LnRzXCIpO1xuIl0sInNvdXJjZVJvb3QiOiIifQ== |
@@ -1,2 +0,2 @@ | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(this,(function(){return(()=>{"use strict";var e={432:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.parseGoogleSheets=void 0,t.parseGoogleSheets=function(e,t){var r=t.feed.entry.reduce((function(e,t){var r=Number(t.gs$cell.row),n=Number(t.gs$cell.col);return e[r]||(e[r]=[]),e[r][n]=t.content.$t,e}),[]).filter((function(e){return e.filter((function(e){return!!e}))})),n=r[0],o=Object.entries(e),u=n.reduce((function(e,t,r){var n,u=null!==(n=o.find((function(e){return e[0],e[1].columnName.toLowerCase()===t.trim().toLowerCase()})))&&void 0!==n?n:[void 0,{type:void 0}],i=u[0],c=u[1].type;return i&&c&&(e[i]={index:r,type:c}),e}),Object.create(null));return r.slice(1).map((function(e){return Object.entries(u).reduce((function(t,r){var n,o=r[0],u=r[1],i=u.index,c=u.type;return t[o]="string"===c?e[i]:(n=e[i],isNaN(Number(n))?void 0:Number(n)),t}),Object.create(null))}))}}},t={};return function r(n){if(t[n])return t[n].exports;var o=t[n]={exports:{}};return e[n](o,o.exports,r),o.exports}(432)})()})); | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(this,(function(){return(()=>{"use strict";var e={432:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.loadGoogleSheets=t.parseGoogleSheets=t.number=t.string=void 0;var n=r(602),o=function(e,t,r){return function(n){return e(n)?t(n):r(n)}},u=function(e){return null==e},i=function(e){return function(){return e}};t.string=o(u,i(void 0),(function(e){return e})),t.number=o(u,i(void 0),(function(e){return isNaN(Number(e))?void 0:Number(e)})),t.parseGoogleSheets=function(e,t){var r=t.feed.entry.reduce((function(e,t){var r=Number(t.gs$cell.row),n=Number(t.gs$cell.col);return e[r]||(e[r]=[]),e[r][n]=t.content.$t,e}),[]).filter((function(e){return e.filter((function(e){return!!e}))})),n=r[0],o=Object.entries(e),u=n.reduce((function(e,t,r){var n,u=null!==(n=o.find((function(e){return e[0],e[1].columnName.toLowerCase()===t.trim().toLowerCase()})))&&void 0!==n?n:[void 0,{type:void 0}],i=u[0],c=u[1].parse;return i&&c&&(e[i]={index:r,parse:c}),e}),Object.create(null));return r.slice(1).map((function(e){return Object.entries(u).reduce((function(t,r){var n=r[0],o=r[1],u=o.index,i=o.parse;return t[n]=i(e[u]),t}),Object.create(null))}))},t.loadGoogleSheets=function(e,r,o){return void 0===o&&(o=1),n.request("https://spreadsheets.google.com/feeds/cells/"+r+"/"+o+"/public/full?alt=json").then((function(e){return e.ok?e.json():e.text().then((function(e){return Promise.reject(e)}))})).then((function(r){return t.parseGoogleSheets(e,r)}))}},602:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.request=void 0,t.request="function"==typeof fetch?fetch:r(Object(function(){var e=new Error("Cannot find module 'node-fetch'");throw e.code="MODULE_NOT_FOUND",e}()))}},t={};return function r(n){if(t[n])return t[n].exports;var o=t[n]={exports:{}};return e[n](o,o.exports,r),o.exports}(432)})()})); | ||
//# sourceMappingURL=parse-googlesheets.min.js.map |
{ | ||
"name": "parse-googlesheets", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Parse google sheets", | ||
@@ -14,3 +14,3 @@ "main": "dist/parse-googlesheets.min.js", | ||
"build": "npx tsc && npx webpack", | ||
"prepublish": "npm run test", | ||
"prepublish": "npm run test && npm run build", | ||
"postversion": "npm publish", | ||
@@ -42,3 +42,6 @@ "postpublish": "git push" | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"@types/node-fetch": "^2.5.7", | ||
"node-fetch": "^2.6.1" | ||
}, | ||
"jest": { | ||
@@ -45,0 +48,0 @@ "collectCoverage": true, |
@@ -0,1 +1,27 @@ | ||
import { request } from './request'; | ||
const ifElse = <T>(expression: Func<[T], boolean>, onTrue: Func<[T], any>, onFalse: Func<[T], any>) => | ||
(data: T) => expression(data) | ||
? onTrue(data) | ||
: onFalse(data); | ||
const isNil = (data: any): data is (null | undefined) => | ||
data === null || data === undefined; | ||
const always = <T>(data: T): Func<[], T> => (): T => data; | ||
const identity = <T>(data: T): T => data; | ||
const toNumberOrNil = (value: string | undefined) => isNaN(Number(value)) | ||
? undefined | ||
: Number(value); | ||
export const string: (data: string | undefined) => string | undefined = ifElse( | ||
isNil, | ||
always(undefined), | ||
identity | ||
); | ||
export const number: (data: string | undefined) => number | undefined = ifElse( | ||
isNil, | ||
always(undefined), | ||
toNumberOrNil | ||
); | ||
export const parseGoogleSheets = <T extends Schema>(schema: T, data: SheetsResponse): Array<Row<T>> => { | ||
@@ -18,17 +44,17 @@ const rows = data.feed.entry.reduce<Array<Array<string>>>((acc: Array<Array<string>>, item) => { | ||
const indexMap = headLine.reduce((acc: Record<string, { index: number; type: 'string' | 'number' }>, sheetsColumnName, index) => { | ||
const [dataName, { type }] = dictionary.find(([key, { columnName }]) => columnName.toLowerCase() === sheetsColumnName.trim().toLowerCase()) ?? [undefined, { type: undefined }]; | ||
const indexMap = headLine.reduce((acc: Record<string, { index: number; parse: Func<[string], any> }>, sheetsColumnName, index) => { | ||
const [dataName, { parse }] = dictionary.find(([key, { columnName }]) => columnName.toLowerCase() === sheetsColumnName.trim().toLowerCase()) ?? [undefined, { type: undefined }]; | ||
if (dataName && type) { | ||
acc[dataName] = { index, type }; | ||
} | ||
if (dataName && parse) { | ||
acc[dataName] = { index, parse }; | ||
} | ||
return acc; | ||
return acc; | ||
}, Object.create(null)); | ||
const toNumber = (value: string): number | undefined => isNaN(Number(value)) ? undefined : Number(value); | ||
return rows.slice(1).map((row) => Object.entries(indexMap).reduce<Row<T>>((acc, [key, { index, type }]: [string, any]) => { | ||
// @ts-ignore | ||
acc[key] = type === 'string' ? row[index] : toNumber(row[index]); | ||
return rows.slice(1).map((row) => Object.entries(indexMap).reduce<Row<T>>((acc, [key, { | ||
index, | ||
parse | ||
}]: [keyof T, { index: number; parse: Func<[string], any> }]) => { | ||
acc[key] = parse(row[index]); | ||
return acc; | ||
@@ -38,14 +64,18 @@ }, Object.create(null))); | ||
export const loadGoogleSheets = <T extends Schema>(schema: T, SheetId: string, SheetListNumber: number = 1): Promise<Array<Row<T>>> => | ||
request(`https://spreadsheets.google.com/feeds/cells/${SheetId}/${SheetListNumber}/public/full?alt=json`) | ||
.then<SheetsResponse>(r => r.ok ? r.json() : r.text().then(message => Promise.reject(message))) | ||
.then(sheets => parseGoogleSheets<T>(schema, sheets)); | ||
type FieldData = { | ||
type: 'string' | 'number'; | ||
parse: Func<[string | undefined], any>; | ||
columnName: string; | ||
} | ||
type Schema = { | ||
export type Schema = { | ||
[Key: string]: FieldData; | ||
} | ||
type Row<T extends Schema> = { | ||
[Key in keyof T]?: T[Key]['type'] extends 'string' ? string : number; | ||
export type Row<T extends Schema> = { | ||
[Key in keyof T]: ReturnType<T[Key]['parse']>; | ||
} | ||
@@ -66,1 +96,3 @@ | ||
} | ||
type Func<Arguments extends Array<any>, Return> = (...args: Arguments) => Return; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31612
11
297
2
6
+ Added@types/node-fetch@^2.5.7
+ Addednode-fetch@^2.6.1
+ Added@types/node@22.10.0(transitive)
+ Added@types/node-fetch@2.6.12(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@4.0.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)