當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript serverUtil.csvRow函數代碼示例

本文整理匯總了TypeScript中utils/server/serverUtil.csvRow函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript csvRow函數的具體用法?TypeScript csvRow怎麽用?TypeScript csvRow使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了csvRow函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: dataExport

async function dataExport() {
    await db.connect()

    const slugs = (await fs.readFile("/Users/mispy/tmp/urls.txt", "utf8")).split("\n").filter(s => s.trim())
    const slugToId = await Chart.mapSlugsToIds()
    const idsToGet = slugs.map(slug => slugToId[slug])

    const variables = await db.query("SELECT v.name, v.id FROM variables v JOIN chart_dimensions cd ON cd.variableId=v.id WHERE cd.chartId IN (?)", [idsToGet])
    const variableIds = variables.map((v: any) => v.id)
    const stream = fs.createWriteStream("/Users/mispy/tmp/sdgs.csv")

    // From dataset CSV export
    const csvHeader = ["Entity", "Year"]
    for (const variable of variables) {
        csvHeader.push(variable.name)
    }

    const columnIndexByVariableId: {[key: number]: number} = {}
    for (const variable of variables) {
        columnIndexByVariableId[variable.id] = csvHeader.indexOf(variable.name)
    }

    stream.write(csvRow(csvHeader))

    const data = await db.query(`
        SELECT e.name AS entity, dv.year, dv.value, dv.variableId FROM data_values dv
        JOIN variables v ON v.id=dv.variableId
        JOIN entities e ON dv.entityId=e.id
        WHERE v.id IN (?)
        ORDER BY e.name ASC, dv.year ASC, dv.variableId ASC`, [variableIds])

    let row: string[] = []
    for (const datum of data) {
        if (datum.entity !== row[0] || datum.year !== row[1]) {
            // New row
            if (row.length) {
                stream.write(csvRow(row))
            }
            row = [datum.entity, datum.year]
            for (const variable of variables) {
                row.push("")
            }
        }

        row[columnIndexByVariableId[datum.variableId]] = datum.value
    }

    // Final row
    stream.write(csvRow(row))

    stream.end()

    await db.end()
}
開發者ID:OurWorldInData,項目名稱:owid-grapher,代碼行數:54,代碼來源:exportSDGTrackerData.ts

示例2: writeCSV

    // Export dataset variables to CSV (not including metadata)
    static async writeCSV(datasetId: number, stream: Writable) {
        const csvHeader = ["Entity", "Year"]
        const variables = await db.query(`SELECT name, id FROM variables v WHERE v.datasetId=? ORDER BY v.columnOrder ASC, v.id ASC`, [datasetId])
        for (const variable of variables) {
            csvHeader.push(variable.name)
        }

        const columnIndexByVariableId: {[key: number]: number} = {}
        for (const variable of variables) {
            columnIndexByVariableId[variable.id] = csvHeader.indexOf(variable.name)
        }

        stream.write(csvRow(csvHeader))

        const data = await db.query(`
            SELECT e.name AS entity, dv.year, dv.value, dv.variableId FROM data_values dv
            JOIN variables v ON v.id=dv.variableId
            JOIN datasets d ON v.datasetId=d.id
            JOIN entities e ON dv.entityId=e.id
            WHERE d.id=?
            ORDER BY e.name ASC, dv.year ASC, v.columnOrder ASC, dv.variableId ASC`, [datasetId])

        let row: string[] = []
        for (const datum of data) {
            if (datum.entity !== row[0] || datum.year !== row[1]) {
                // New row
                if (row.length) {
                    stream.write(csvRow(row))
                }
                row = [datum.entity, datum.year]
                for (const variable of variables) {
                    row.push("")
                }
            }

            row[columnIndexByVariableId[datum.variableId]] = datum.value
        }

        // Final row
        stream.write(csvRow(row))

        stream.end()
    }
開發者ID:OurWorldInData,項目名稱:owid-grapher,代碼行數:44,代碼來源:Dataset.ts

示例3: main

async function main() {
    const trendsPath = "/tmp/trends.json"

    let data: {[key: string]: any} = {}

    // Only fetch for queries we don't already have
    if (fs.pathExistsSync(trendsPath)) {
        data = JSON.parse(await fs.readFile("/tmp/trends.json", "utf8"))
    }

    const titles = await getTitles()
    for (const title of titles) {
        if (title in data)
            continue

        console.log(title)

        try {
            const related = await googleTrends.relatedQueries({ keyword: title })
            data[title] = related
        } catch (err) {
            data[title] = JSON.stringify({})
            console.error(err)
        }

        await fs.writeFile('/tmp/trends.json', JSON.stringify(data))
    }

    console.log("DONE")

    const rows: string[][] = [[]]
    let i = 0
    for (const title in data) {
        const datum = JSON.parse(data[title])
        if (datum && datum.default && datum.default.rankedList[0].rankedKeyword.length) {
            rows[0][i] = title
            // const ranked = datum.default.rankedList[0].rankedKeyword.map((v: any) => `${v.value} ${v.query}`)
            let j = 1
            for (const keyword of datum.default.rankedList[0].rankedKeyword) {
                if (!rows[j])
                    rows[j] = []
                rows[j][i] = `${keyword.value} ${keyword.query}`
                j += 1
            }
            i += 1
        }
    }

    let csv = ""
    for (const row of rows) {
        csv += csvRow(row)
    }

    await fs.writeFile('/tmp/trends.csv', csv)
}
開發者ID:OurWorldInData,項目名稱:owid-grapher,代碼行數:55,代碼來源:googleTrendsAnalysis.ts

示例4: csvRow

 ...data.map(json => csvRow(headers.map((header) => json[header])))
開發者ID:OurWorldInData,項目名稱:owid-grapher,代碼行數:1,代碼來源:exportStripeDonors.ts

示例5: toCSV

function toCSV(headers: string[], data: any[]) {
    return [
        csvRow(headers),
        ...data.map(json => csvRow(headers.map((header) => json[header])))
    ].join('') // csvRow() already adds newlines
}
開發者ID:OurWorldInData,項目名稱:owid-grapher,代碼行數:6,代碼來源:exportStripeDonors.ts


注:本文中的utils/server/serverUtil.csvRow函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。