Skip to content

Instantly share code, notes, and snippets.

View nadeesha's full-sized avatar

Nadeesha Cabral nadeesha

View GitHub Profile
private double FuzzyStringComparison(string source, string target)
{
// if both of the strings are empty, return 0
// else if the source is empty but not the target, the distance is the target length
// else if the target is empty and not the source, the the distance is the source length.
if (string.IsNullOrEmpty(source))
{
if (string.IsNullOrEmpty(target))
{
return 0;
@nadeesha
nadeesha / Markdium-JSX.jsx
Created October 22, 2021 20:22
Markdium-Using a .netrc to securely store remote server credentials
$cat ~/.netrc
machine api.usecloudstate.io
login me@example.com
password c4cd94da15ea0544802c2cfd5ec4ead324327111
machine github.com
login me@example.com
password c2224da15ea0544802c2cfd5ec4ead324327430
@nadeesha
nadeesha / callable-generic.ts
Created July 2, 2017 20:39
Callable generic params in TS
// from: http://www.typescriptlang.org/play/#src=function%20someFactory%3CT%3E()%20%7B%0D%0A%20%20return%20class%20%7B%0D%0A%20%20%20%20method(someParam%20%3A%20T)%20%7B%0D%0A%20%20%20%20%20%20%0D%0A%20%20%20%20%7D%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0A%2F%2F%20how%20to%20invoke%20this%20factory%20with%20a%20defined%20%3CT%3E%3F%0D%0A%0D%0Aconst%20SomeClass%20%3D%20someFactory%3C%7B%20whatever%3A%20string%20%7D%3E()%0D%0Aconst%20someInstance%20%3D%20new%20SomeClass()%0D%0AsomeInstance.method('will-error-here')%0D%0A
function someFactory<T>() {
return class {
method(someParam : T) {
}
}
}
export function tryCatch<Props, Result>({
tryer,
catcher
}: {
tryer: (props: Props) => Result;
catcher: (props: Props, message: string) => Result;
}) {
return (props: Props) => {
try {
return tryer(props);
const successNoContent = [
(data: string) => data.length === 0,
() => 204,
];
const successWithContent = [
(data: string) => data.length > 0,
() => 200
];
import { encaseP, node, encase, map, chain } from "fluture";
const readFileF = filePath => node(done => fs.readFile(filePath, "utf8", done));
const fetchF = url => encaseP(url => fetch(url));
const jsonParseF = encase(jsonString => JSON.parse(jsonString));
const getPackageDownloads = (npmPackageName, onSuccess, onFailure) => {
readFileF("package.json")
const hasGoodRating = rating => rating > 4;
const priceChange = conditionally({
if: hasGoodRating,
then: rating => 1000 * rating,
else: () => 1000,
});
const getDescription = conditionally({
if: hasGoodRating,
export function tryCatch({
tryer,
catcher
}) {
return (props) => {
try {
return tryer(props);
} catch (e) {
return catcher(props, e.message);
}
const storeLanguageCode = tryCatch({
tryer: (languageCode) => {
window.localStorage.setItem("LANG_CODE", languageCode);
return true;
},
catcher: (languageCode, errorMessage) => {
logger.log(`${errorMessage} <-- happened while storing ${languageCode}`);
return false;
}
});
function setUserLanguageCode(selectedLanguage) {
const languageCode = getLanguageCode(selectedLanguage);
let storedSuccessfully;
try {
window.localStorage.setItem("LANG_CODE", languageCode);
storedSuccessfully = true;
} catch (e) {
storedSuccessfully = false;