This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Removes all the occurences of a particular property from an object | |
*/ | |
function stripeObject(obj, property="_meta") { | |
Object.keys(obj).forEach(k => { | |
if (typeof obj === "object") { | |
if (k === property) { | |
delete obj[k]; | |
} else { | |
obj[k] = stripeObject(obj[k]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Customize the json-server response to match the Django REST Framework pagination style. | |
* | |
* @author Jesús Moreno Amor <[email protected]>. | |
* @version 1.0.0 | |
*/ | |
const jsonServer = require('json-server') | |
const server = jsonServer.create() | |
const router = jsonServer.router('projects.json') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Input array | |
const peopleArray = [ | |
{ id: 123, name: "dave", age: 23 }, | |
{ id: 456, name: "chris", age: 23 }, | |
{ id: 789, name: "bob", age: 23 }, | |
{ id: 101, name: "tom", age: 23 }, | |
{ id: 102, name: "tim", age: 23 } | |
] | |
// Simple function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Returns the value of a nested javascript reference path | |
*/ | |
function nestedValue(obj, path) { | |
return path.replace(/\[(\d+)\]/g, '.$1').split('.').reduce((previous, current) => { | |
return previous && previous.hasOwnProperty(current) ? previous[current] : null; | |
}, obj); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Deep finds a key within a XML to JSON transformed structure | |
*/ | |
function deepFind(object, path) { | |
let current = object; | |
path.split('.').forEach((e, i) => { | |
if (current[e] == undefined) { return null } | |
current = current[e]; | |
}); | |
if (current['$'] !== undefined) { return current['$'] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* XML to JSON transformation sets one item lists to an object | |
* this function forces value to be a list instead of an object | |
*/ | |
function assureArray(item) { | |
return (item ? (Array.isArray(item) ? item : [item]) : []); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import unicode_literals | |
from django.db import models, migrations | |
from django.contrib.auth.models import User | |
def create_superuser(apps, schema_editor): | |
User.objects.create_superuser('admin', '[email protected]', 'admin') | |
def delete_superuser(apps, schema_editor): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from apps.projects.models import Project | |
from django.db.models import Count | |
# Find duplicated models based on the value of one or more fields | |
dupes = Project.objects.values('name').annotate(Count('id')).order_by().filter(id__count__gt=1) | |
dupes = Project.objects.values('name', 'code').annotate(Count('id')).order_by().filter(id__count__gt=1) | |
dupes = Project.objects.values('name', 'code', 'release').annotate(Count('id')).order_by().filter(id__count__gt=1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.dom.minidom | |
def pretty(xml_string): | |
""" Prettify and compact an XML string """ | |
xml_string = xml.dom.minidom.parseString(xml_string).toprettyxml(indent=" ").split('\n') | |
return '\n'.join([line for line in xml_string if line.strip()]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Find which process is using a port | |
tasklist | grep $(netstat -noa | grep 30002 | awk '{print $NF}') | awk '{print $2}' |