Skip to content

Instantly share code, notes, and snippets.

View manhnguyenv's full-sized avatar

Manh Nguyen manhnguyenv

View GitHub Profile
@manhnguyenv
manhnguyenv / slugify.js
Created July 26, 2018 04:00 — forked from codeguy/slugify.js
Create slug from string in Javascript
function string_to_slug (str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
@manhnguyenv
manhnguyenv / slugify.js
Created July 26, 2018 04:00 — forked from mathewbyrne/slugify.js
Javascript Slugify
function slugify(text)
{
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
@manhnguyenv
manhnguyenv / aes-example.cs
Created January 7, 2019 04:49 — forked from yetanotherchris/aes-example.cs
C# AES asymmetric encryption and decryption example
string ivAsBase64;
string encryptedTextAsBase64;
string keyAsBase64;
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
// Store the IV (they can be stored if you don't re-use a key)
aes.GenerateIV();
byte[] iv = aes.IV;
ivAsBase64 = Convert.ToBase64String(iv);
@manhnguyenv
manhnguyenv / comaprison.cs
Created April 5, 2019 07:44 — forked from huseyint/comaprison.cs
Property equality comparison on two objects
// Shamelessly stolen and adapted from http://stackoverflow.com/questions/506096/comparing-object-properties-in-c
public static class Comparisons
{
public static bool PublicInstancePropertiesEqual<T>(this T self, T to, params string[] ignore) where T : class
{
if (self != null && to != null)
{
var type = typeof(T);
foreach (var propertyName in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Select(pi => pi.Name).Except(ignore))
{
@manhnguyenv
manhnguyenv / classes.js
Created April 5, 2019 09:08
pure javascript add class and remove class functions
var els = document.getElementsByClassName('current-class-name');
removeClass(els, 'current-class-name');
addClass(els, 'new-class-name');
var el = document.getElementById('current-class-name');
removeClass([el], 'current-class-name');
addClass([el], 'new-class-name');
function addClass(elements, className) {
for (var i = 0; i < elements.length; i++) {
@manhnguyenv
manhnguyenv / UserWithCrypto.js
Created April 30, 2019 15:51 — forked from pdonham/UserWithCrypto.js
Sample code for Using JWTs for Authentication in RESTful Applications (http://sites.bu.edu/perryd/?p=259)
/*
For this version we'll add a register method to add a user to the database using proper
password encryption. It also demonstrates how to add and use a method on a schema.
Reference: https://www.sitepoint.com/user-authentication-mean-stack
*/
const mongoose = require('mongoose')
const crypto = require('crypto')
//const findOrCreate = require('mongoose-findorcreate')
//Set up ES6 Promises
@manhnguyenv
manhnguyenv / Slug.html
Created July 19, 2019 06:57
Generating Slugs with JavaScript (Slug.js)
<!DOCTYPE html>
<html>
<body>
<p>Click the button to return the value of the string object.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
@manhnguyenv
manhnguyenv / toSlug.js
Created July 19, 2019 06:58
JavaScript toSlug.js
String.prototype.toSlug = function(){
st = this.toLowerCase();
st = st.replace(/[\u00C0-\u00C5]/ig,'a')
st = st.replace(/[\u00C8-\u00CB]/ig,'e')
st = st.replace(/[\u00CC-\u00CF]/ig,'i')
st = st.replace(/[\u00D2-\u00D6]/ig,'o')
st = st.replace(/[\u00D9-\u00DC]/ig,'u')
st = st.replace(/[\u00D1]/ig,'n')
st = st.replace(/[^a-z0-9 ]+/gi,'')
st = st.trim().replace(/ /g,'-');
@manhnguyenv
manhnguyenv / location.html
Created July 19, 2019 07:00 — forked from mathewbyrne/location.html
A small, single-page tool for getting your current geolocation.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Navigation Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script>
var isTouchDevice = 'ontouchstart' in document.documentElement;
window.onload = function () {
var where = document.getElementById('where');
@manhnguyenv
manhnguyenv / code-block.html
Created July 19, 2019 07:01 — forked from mathewbyrne/code-block.html
A fun little experiment playing with CSS box shadows. I think it roughly works in Safari, Chrome and Firefox. No guarantees though.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Shadows Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>
* {