Skip to content

Instantly share code, notes, and snippets.

@tysonnero
tysonnero / Cookies.js
Created July 27, 2017 03:10
ES6 Cookie Library
export function getCookie(name) {
if (!name || !hasItem(name)) { return null; }
return decodeURIComponent(document.cookie.replace(new RegExp(`(?:^|.*;\\s*)${encodeURIComponent(name).replace(/[\-\.\+\*]/g, '\\$&')}\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*`), '$1'));
}
export function setCookie(name, value, options) {
if (!name || /^(?:expires|max\-age|path|domain|secure)$/.test(name)) { return; }
const { expires, path, domain, secure } = options;
let expiresAttr = '';
@tysonnero
tysonnero / user-schema-pre-validate-test.js
Created August 19, 2014 15:30
Unit Test for User Model Pre-validate Hook
it('should validate when password strength passes', function () {
var user = new User({ name: 'Test', username: 'Test', password: 'Test@123' });
user.validate();
assert.isUndefined(user.errors);
});
it('should invalidate when password strength fails', function () {
var user = new User({ name: 'Test', username: 'Test', password: '1234' });
user.validate();
assert.isDefined(user.errors.password);
@tysonnero
tysonnero / user-schema-pre-validate.js
Created August 19, 2014 15:20
UserSchema Pre-Validate Method
// Check password strength using OWASP module
UserSchema
.pre('validate', function (next) {
// Set options
owasp.config(config.password);
var result = owasp.test(this.password);
if (result.errors.length) {
// Join all errors in the array so only one error is returned
@tysonnero
tysonnero / user-schema.js
Last active August 29, 2015 14:05
User Schema for Mongoose Pre-validation Hook Testing
var UserSchema = new Schema({
name: { type: String, required: true },
username: { type: String, required: true, index: { unique: true } },
email: String,
role: {
type: String,
default: 'User'
},
password: { type: String, required: true }
});
@tysonnero
tysonnero / AutoFacConfig.cs
Last active December 15, 2015 17:39
Self-Host Web API and Controllers in Class Libraries with Autofac: http://www.codemonkeez.com/2013/04/self-host-web-api-and-controllers-in.html
public class AutoFacConfig
{
public static void Initialize(HttpConfiguration config)
{
Initialize(config, RegisterServices(new ContainerBuilder()));
}
public static void Initialize(HttpConfiguration config, IContainer container)
{
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);