Skip to content

Instantly share code, notes, and snippets.

View svdamani's full-sized avatar
🎯
Focusing

Shreevardhan svdamani

🎯
Focusing
View GitHub Profile
@svdamani
svdamani / bing.py
Created April 6, 2021 11:25
Bing wallpaper as desktop background on macOS
#!/usr/local/bin/python3
from sys import argv
from os import makedirs, system, path
from json import loads
from urllib.request import urlopen, urlretrieve
from subprocess import Popen, PIPE
def ascript(cmd):
return Popen(['osascript', '-', '2', '2'], stdin=PIPE, stdout=PIPE, stderr=PIPE, encoding='utf-8').communicate(cmd)
@svdamani
svdamani / xmlToJson.js
Last active February 27, 2020 07:23
Convert XML document to JSON
// const xmlDoc = new DOMParser().parseFromString(xmlString, 'text/xml');
function xmlToJson(xml) {
const parseAll = s => (s && ((!isNaN(s) && parseFloat(s)) || (Date.parse(s) && new Date(s)))) || s.trim();
const nodesToJson = function(nodes) {
return Array.from(nodes || [])
.map(item => ({
name: item.nodeName,
value: item.nodeType === Node.ATTRIBUTE_NODE ? parseAll(item.nodeValue) : xmlToJson(item)
}))
.filter(item => item.value)
@svdamani
svdamani / magicSquare.js
Created January 28, 2019 09:00
Ramanujan's Magic Square
function magicSquare(date) {
date = date instanceof Date ? date : new Date(date);
var d = date.getDate(),
m = date.getMonth() + 1,
c = ~~(date.getFullYear() / 100),
y = date.getFullYear() % 100;
return [
[d, m, c, y],
[y + 1, c - 1, m - 3, d + 3],
[m - 2, d + 2, y + 2, c - 2],
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { DOCUMENT } from '@angular/common';
import { Directive, ElementRef, Inject, Input, OnInit } from '@angular/core';
@Directive({ selector: '[myAutoFocus], [autofocus]' })
export class AutoFocusDirective implements OnInit {
private host: HTMLElement;
private focused: Element;
private autoFocus = true;
@svdamani
svdamani / rest.interceptor.ts
Created August 18, 2018 10:49
Angular Http Interceptor
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable()
export class RestInterceptor implements HttpInterceptor {
baseUrl: string; // base API url
token: string; // Initialize from login
@svdamani
svdamani / rest.service.ts
Created August 18, 2018 10:37
Angular Http wrapper
import { Injectable } from '@angular/core';
import { Http, RequestOptionsArgs, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class RestService {
baseUrl: string; // base API url
token: string; // Initialize from login
constructor(private http: Http) {}
@svdamani
svdamani / safeGet.js
Last active April 3, 2023 13:17
Simple JS object safe property accessor function
function safeGet(obj, props, defaultValue) {
if (typeof props === 'string') {
props = props.split('.');
}
function safeGetByArray(obj, propsArray, defaultValue) {
if (obj === undefined || obj === null) {
return defaultValue;
}
if (propsArray.length === 0) {
return obj;
public static class SqlConnectionExtensions {
public static SqlCommand CreateCommand(this SqlConnection connection, string cmdText, CommandType cmdType = CommandType.Text, params SqlParameter[] parameters) {
var cmd = connection.CreateCommand();
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
if (parameters != null)
foreach (var parameter in parameters)
cmd.Parameters.Add(parameter);
return cmd;
}
<style type="text/css">
input,
select,
textarea {
font-size: 1rem;
padding: 5px 10px;
height: 35px;
border-radius: 4px;
border: 1px solid #bbb;
outline: none;
@svdamani
svdamani / ajax.js
Last active April 3, 2023 13:14
Simple XMLHttpRequest helper function
function ajax(method, url) {
var x = (function () {
return window.XMLHttpRequest
? new XMLHttpRequest()
: window.ActiveXObject
? new ActiveXObject("Microsoft.XMLHTTP")
: null;
})();
x.open(method, url, true);