Skip to content

Instantly share code, notes, and snippets.

View jmorenoamor's full-sized avatar
🚀
Software craftsman, Master of nothing. I love space stuff.

Jesús Moreno Amor jmorenoamor

🚀
Software craftsman, Master of nothing. I love space stuff.
View GitHub Profile
@jmorenoamor
jmorenoamor / stripe_object.js
Created April 5, 2020 07:13
Removes all the occurences of a particular property from an object
/*
* 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]);
@jmorenoamor
jmorenoamor / json-server_django_drf_pagination.js
Created March 29, 2020 07:37
Customize the json-server response to match the Django REST Framework pagination style
/**
* 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')
@jmorenoamor
jmorenoamor / arrray_to_object.js
Created March 29, 2020 07:24
Transform an array of objets to an object by selected key
// 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
@jmorenoamor
jmorenoamor / nested_value.js
Last active March 15, 2020 11:26
Returns the value of a nested javascript reference path
/*
* 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);
}
@jmorenoamor
jmorenoamor / deep_find.js
Created March 15, 2020 09:38
Deep finds a key within a XML to JSON transformed structure
/*
* 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['$'] }
@jmorenoamor
jmorenoamor / assure_array.js
Created March 15, 2020 09:38
XML to JSON transformation sets one item lists to an object, this function forces a value to be a list instead of an object
/*
* 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]) : []);
}
@jmorenoamor
jmorenoamor / 0002_create_superuser.py
Created February 2, 2018 10:01
Create superuser in migration
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):
@jmorenoamor
jmorenoamor / django_duplicates.py
Created July 26, 2017 08:36
Find duplicate models using Django ORM
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)
@jmorenoamor
jmorenoamor / pretty_xml.py
Created May 5, 2017 05:16
Prettify and compact an XML string
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()])
@jmorenoamor
jmorenoamor / cygwin_scripts.sh
Created August 20, 2014 07:31
Cygwing scripts
# Find which process is using a port
tasklist | grep $(netstat -noa | grep 30002 | awk '{print $NF}') | awk '{print $2}'