Skip to content

Instantly share code, notes, and snippets.

Avatar

Nordin-010 Nordin-010

  • Netherlands, Rotterdam
View GitHub Profile
@Nordin-010
Nordin-010 / userSchema.js
Created May 18, 2020 08:29 — forked from jzellis/userSchema.js
Boilerplate for simple Mongoose user schema
View userSchema.js
/*
I find myself recreating user models for Mongoose every time I start a new project, so I thought I'd create a generic schema for a user model that can be added to or modified as need be.
This is loosely based on the Meteor user model (using a "profile" sub-object for the user's personal information). It also includes an optional geolocation point for the user, and Mongoose timestamps, as well as a pre("save") function to bcrypt the user password and a comparePassword() function.
Just save this file wherever you store your models and do something like const Users = include('./models/userSchema.js') and you can just use it as a standard Mongoose user model.
The username/email address definitions were copied from this tutorial: https://thinkster.io/tutorials/node-json-api/creating-the-user-model
@Nordin-010
Nordin-010 / nsnotification.m
Last active September 30, 2019 12:56
NSNotification example
View nsnotification.m
- (void) subscribeNotificationWithName: (NSString*) name {
[ [NSNotificationCenter defaultCenter ] addObserver: self
selector: @selector(didReceiveNotification:)
name: name
object: nil ];
}
- (void) didReceiveNotification: (NSNotification *) notification {
NSLog(@"didReceiveNotification");
@Nordin-010
Nordin-010 / ifelse.swift
Last active April 9, 2019 09:24
How to improve this snippet of Swift code. I have like more than 10 checkboxes
View ifelse.swift
var socialData: SocialData? = storage.data("social")
...
...
// checkLiveAudio = NSButton (CheckBox button)
...
if let data = socialData {
if data.isAudioEnabled() {
checkLiveAudio.state = NSControl.StateValue.on
} else {
checkLiveAudio.state = NSControl.StateValue.off
@Nordin-010
Nordin-010 / optionals.swift
Last active March 20, 2019 10:11
Optionals
View optionals.swift
// variables declaration
var str = "Hello, playground"
var x = Int(10)
// class definition
class ParentClass {
func writeText(_ text: String?) -> Void {
print("\(text ?? "empty text")")
}
}
View route-with-timelogger.js
var express = require('express')
var router = express.Router()
// middleware that is specific to this router
// this will be called for every request (get, put, update and delete)
router.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now())
next() // pass control to the nex router int he chain
})
// define the home page route
View route-with-multiple-handlers.js
var cb0 = function (req, res, next) {
console.log('CB0')
next() // don't forget to call next()!
}
var cb1 = function (req, res, next) {
console.log('CB1')
next() // don't forget to call next()!
}
View route-with-2-handlers.js
// route handler with 2 handlers
route.get('/example/b', function (req, res, next) { // handler 1
console.log('the response will be sent by the next function ...')
next() // don't forget to call next() for passing control to the nex callback
}, function (req, res) { // handler 2
res.send('Hello from B!')
})
View async-await2.js
/**
* This function returns a promise to fetch a user by id
*/
function getUserWithId(id) {
return new Promise((resolve, reject) => {
// Look for a user with id in the database
User.find({_id: id}, (user, err) => {
if(err) return reject(err);
// on success
View async-await.js
/**
* promise object
*/
function getUserWithId(id) {
return new Promise((resolve, reject) => {
// Look for a user with id in the database
User.find({_id: id}, (user, err) => {
if(err) return reject(err);
@Nordin-010
Nordin-010 / promise_example2.js
Created January 15, 2019 13:15
Here we're chaining multiple promises, which solves the callback hell.
View promise_example2.js
/**
* This function returns a promise to fetch a user by id
*/
function getUserWithId(id) {
return new Promise((resolve, reject) => {
// Look for a user with id in the database
User.find({_id: id}, (user, err) => {
if(err) return reject(err);
// on success