Skip to content

Instantly share code, notes, and snippets.

Avatar

Nikhil Vijayan nkhil

View GitHub Profile
@nkhil
nkhil / for-each-async-await.js
Created Jun 13, 2021
Do not use async/await inside forEach functions
View for-each-async-await.js
async function someAsyncFuncion(num) {
return await new Promise(resolve => resolve(num))
}
function orchestrator() {
[1, 2, 3, 4, 5].forEach(async num => {
console.log('console 1 fired')
const promiseResolvedValue = await someAsyncFuncion(num)
console.log(promiseResolvedValue)
console.log('console 2 fired')
@nkhil
nkhil / Calculator.md
Last active Jun 9, 2021
Create a simple calculator that given a string of operators (), +, -, *, / and numbers separated by spaces returns the value of that expression
View Calculator.md
View s3-test.test.js
const s3 = require('./s3') // This is the file where we use aws-sdk's S3 CopyObject method
const mockS3Instance = {
copyObject: jest.fn().mockReturnThis(),
promise: jest.fn().mockReturnThis(),
catch: jest.fn(),
}
jest.mock('aws-sdk', () => {
return { S3: jest.fn(() => mockS3Instance) }
View s3.js
const AWS = require('aws-sdk')
// a function that returns a promise
function copyFileToS3() {
// initialise AWS S3
const s3 = new AWS.S3({
accessKeyId: 'whatever',
secretAccessKey: 'whatever',
endpoint: 'http://localhost:4566', // localstack is running on port 4566. Imagine this was the real S3 endpoint
View agenda.md

Agenda investigation spike

Attempt 1:

BEHAVIOUR: This enforces only one instance carrying out jobs at a time

const Agenda = require('agenda');
View logger.spec.js
const path = require('path');
const { spawn } = require('child_process');
// Run using Jest
describe('logger behaviour', () => {
it('logs out multiple params - 2 strings', done => {
const testAppFilePath = path.join(
__dirname,
'../logger.js',
@nkhil
nkhil / logger.js
Last active Feb 2, 2021
testing logging to stdout in node - test setup
View logger.js
const logger = require('./logger.js');
logger.info({ foo: 'bar' }, 'param2');
// Outputs:
// {"level":30,"time":1612298595613,"msg":"param2","pid":3039,"hostname":"whatever","foo":"bar","v":1}
View sum_of_pairs.md

Sum of Pairs

Given a list of integers and a single sum value, return the first two values (parse from the left please) in order of appearance that add up to form the sum. CodeWars kata link

Note: The following solution is not by me. It was one of the first solutions in the showcase under 'Solutions'. Apologies for not attributing the authors.

var sum_pairs = function (ints, s) {
@nkhil
nkhil / s3.md
Last active Oct 8, 2020
Creating copies of files
View s3.md

Create bucket 'test'

awslocal s3api create-bucket --bucket test --region us-east-1

Copy local file into S3

awslocal s3 cp test.txt s3://test/test.txt
@nkhil
nkhil / longest_palindrome.md
Last active Sep 14, 2020
Given a string, find the longest palindrome (JavaScript)
View longest_palindrome.md

Find the longest palindrome (JavaScript)

This is my solution to the longest palindrome kata on Code Wars.

Solution

  function reverseString(st) {
    return st.split('').reverse().join('')
  }