Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions composer/composerJSTutorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable func-style */
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module.exports = function main(
line = 'tampa, 106, january, null, null, 08--17-2019'
) {
// [START transform_CSV_to_JSON]

function transformCSVtoJSON(line) {
const values = line.split(',');
const properties = [
'location',
'average_temperature',
'month',
'inches_of_rain',
'is_current',
'latest_measurement',
];
const weatherInCity = {};

for (let count = 0; count < values.length; count++) {
if (values[count] !== 'null') {
weatherInCity[properties[count]] = values[count];
}
}

const jsonString = JSON.stringify(weatherInCity);
return jsonString;
}

transformCSVtoJSON(line);
// [END transform_CSV_to_JSON]
return transformCSVtoJSON(line);
};
22 changes: 22 additions & 0 deletions composer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "nodejs-docs-samples-composer",
"description": "Node.js Google Composer sample.",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "mocha --exit test/*.test.js"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^7.0.0"
}
}
15 changes: 15 additions & 0 deletions composer/test/composerJSTutorial.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const {assert} = require('chai');
const tutorial = require('../composerJSTutorial.js');

const lineTest = 'tampa, 106, january, null, null, 08-17-2019';

it('should separate out a line and turn into a json string', () => {
const output = tutorial(lineTest);
console.log(output);
assert.match(
output,
new RegExp(
'{"location":"tampa","average_temperature":" 106","month":" january","inches_of_rain":" null","is_current":" null","latest_measurement":" 08-17-2019"}'
)
);
});