|
| 1 | +/* eslint-disable func-style */ |
| 2 | +// Copyright 2020 Google LLC |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +module.exports = function main( |
| 17 | + line = 'tampa, 106, january, null, null, 08-17-2019' |
| 18 | +) { |
| 19 | + // [START composer_transform_csv_to_json] |
| 20 | + |
| 21 | + function transformCSVtoJSON(line) { |
| 22 | + const values = line.split(','); |
| 23 | + const properties = [ |
| 24 | + 'location', |
| 25 | + 'average_temperature', |
| 26 | + 'month', |
| 27 | + 'inches_of_rain', |
| 28 | + 'is_current', |
| 29 | + 'latest_measurement', |
| 30 | + ]; |
| 31 | + const weatherInCity = {}; |
| 32 | + |
| 33 | + for (let count = 0; count < values.length; count++) { |
| 34 | + if (values[count] !== 'null') { |
| 35 | + weatherInCity[properties[count]] = values[count]; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + const jsonString = JSON.stringify(weatherInCity); |
| 40 | + return jsonString; |
| 41 | + } |
| 42 | + |
| 43 | + transformCSVtoJSON(line); |
| 44 | + // [END composer_transform_csv_to_json] |
| 45 | + return transformCSVtoJSON(line); |
| 46 | +}; |
0 commit comments