Skip to content

Instantly share code, notes, and snippets.

View jwadhwani's full-sized avatar

Jayesh Wadhwani jwadhwani

  • IntraPoint, Inc, IntraPoint AS
  • Mountain View, CA, USA. Oslo, Norway.
  • X @CobraIOT
View GitHub Profile
@jwadhwani
jwadhwani / gist:be18212f2f34b0470f7a105506af3d11
Created March 24, 2019 02:22
String to first letter uppercase, rest lower case
const toFirstUpperRestLowerCase = (str) => {
return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
};
const s = 'POINT';
console.log(toFirstUpperRestLowerCase(str)); //Point
@jwadhwani
jwadhwani / gist:5056c45255f23da755000331bce47f73
Created June 11, 2018 17:31
JavaScript/NodeJS equivalent of PHP's uniqid with a little extra entropy.
"use strict";
const crypto = require('crypto'),
hash = crypto.createHash('md5');
const uniqid = (prefix = '') => {
return `${prefix}${hash.update(Date.now().toString()).digest('hex')}`;
};
console.log(uniqid('Example_Prefix-')); //Example_Prefix-d6caf083d4676ec0f2cf2a9a3b2b3020
@jwadhwani
jwadhwani / gist:d167fe7c926bf2808b5661b5169c0b54
Created June 12, 2017 17:03
REGEXP(JS): Replace everything in a string except for upper case letters and commas
const c = "A?A,V V,F2F, MM".replace( /[^A-Z,]/g, '');
console.log(c); //AA,VV,FF,MM
@jwadhwani
jwadhwani / gist:3ced698a6fd285fd14183bc66a2f98a7
Last active June 12, 2017 16:55
REGEXP(JS): Replace one or more commas at the end of a string
let s= "AA,VV,FF,".replace( /,+$/, '');
console.log(s); //AA,VV,FF
let x = "AA,VV,FF,,".replace( /,+$/, '');
console.log(x); //AA,VV,FF
@jwadhwani
jwadhwani / PhantomJSCookie.js
Last active November 6, 2016 00:47
Get a cookie from PhantomJS/CasperJS
//phantom.cookies is an array of objects
//if cookie not available null is returned.
function getCookie (name) {
var ret = null;
phantom.cookies.forEach(function (v) {
if (v.name === name) {
ret = v.value;
}
});
@jwadhwani
jwadhwani / gist:20f63e56c764bf0d57c5c6f9f00ce048
Last active April 4, 2016 03:28
Check if a directory exists in NodeJS
//sync
try{
fs.statSync('../testing');
}catch(e){
console.log(e.message);//ENOENT: no such file or directory, stat '../testing'
}
//async
fs.stat('../testing', function(err, res){
if(err){
@jwadhwani
jwadhwani / gist:29b92ec89c42a79bf00b
Last active July 24, 2017 09:24
is_json() for PHP
<?php
function is_json($value = null){
$ret = true;
if(null === @json_decode($value)){
$ret = false;
}
return $ret;
}
@jwadhwani
jwadhwani / gist:a0d514bae32505ba8b55
Last active February 7, 2016 15:48
A snippet in PHP using preg_replace to remove excess line feeds
<?php
$data = preg_replace('/\x0a+/', "\x0a", $data);