Last active
April 13, 2016 20:00
-
-
Save Dviejopomata/c17218dbddcedf07470852d0b56dfd0a to your computer and use it in GitHub Desktop.
This file contains 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 "reflect-metadata"; | |
import * as moment from "moment"; | |
import * as _ from "lodash"; | |
moment.locale("es"); | |
try { | |
const formatMetadataKey = Symbol("churro"); | |
enum TIPOS { | |
tributo, | |
importe, | |
string, | |
number, | |
date | |
} | |
function Churro(n: number) { | |
return Reflect.metadata(formatMetadataKey, n); | |
} | |
function getFormat(target: any, propertyKey: string) { | |
return Reflect.getMetadata(formatMetadataKey, target, propertyKey); | |
} | |
class Tributo { | |
LETRA: number; | |
@Churro(1) | |
SIGLAS: string; | |
@Churro(0) | |
LITERALVIA: string; | |
@Churro(2) | |
NUMVIA: number; | |
@Churro(3) | |
PLANTA: number; | |
@Churro(4) | |
PUERTA: string; | |
constructor(siglas: string, literal: string, NUMVIA: number, PLANTA: number, PUERTA: string) { | |
this.SIGLAS = siglas; | |
this.LITERALVIA = literal; | |
this.NUMVIA = NUMVIA; | |
this.PLANTA = PLANTA; | |
this.PUERTA = PUERTA; | |
this.LETRA = 1; | |
} | |
getChurroNumber(property) { | |
var n = getFormat(this, property); | |
return n; | |
} | |
} | |
const K_SEPARADOR = "|"; | |
var churro = "DAVID VIEJO1|21|21/12/1994|22.12|ALFONSO EL SABIO$AV$44$1$A|MAISSONAVE$AV$21$4$A|FOO NAME|49|2/12/2012|33.13|MAISSONAVE$AV$21$4$A|LFONSO EL SABIO$AV$13$6$D"; | |
var fields: { name: string, type: TIPOS, format?: string, separador?: string, clase?: Object }[] = [ | |
{ | |
name: "NOMBRE", | |
type: TIPOS.string, | |
}, | |
{ | |
name: "EDAD", | |
type: TIPOS.number, | |
}, | |
{ | |
name: "FECHA", | |
type: TIPOS.date, | |
format: "DD/MM/YYYY" | |
}, | |
{ | |
name: "IMPORTE", | |
type: TIPOS.importe | |
}, | |
{ | |
name: "TRIBUTO", | |
type: TIPOS.tributo, | |
separador: "$", | |
clase: Tributo.prototype | |
}, | |
{ | |
name: "TRIBUTOVINCULADO", | |
type: TIPOS.tributo, | |
separador: "$", | |
clase: Tributo.prototype | |
} | |
] | |
var array = churro | |
.split(K_SEPARADOR) | |
.reduce(function (acc, act, index) { | |
var division = index / fields.length; | |
var indx = parseInt(division.toString(), 10); | |
var campoIndex = index % fields.length; | |
var campo = fields[campoIndex]; | |
var obj = acc[indx]; | |
obj = obj || {}; | |
switch (campo.type) { | |
case TIPOS.string: | |
obj[campo.name] = act; | |
break; | |
case TIPOS.number: | |
obj[campo.name] = parseInt(act); | |
break; | |
case TIPOS.date: | |
obj[campo.name] = moment(act, campo.format).toDate(); | |
break; | |
case TIPOS.importe: | |
obj[campo.name] = parseFloat(act); | |
break; | |
case TIPOS.tributo: | |
var splitted = act.split(campo.separador); | |
var clas = Object.create(campo.clase); | |
clas.constructor.apply(clas, splitted); | |
var props = Object.getOwnPropertyNames(clas); | |
var fullProps = props | |
.map((key) => { | |
return { name: key, position: clas.getChurroNumber(key) } | |
}).filter(i => i.position !== undefined); | |
var propsToGet = fullProps.map(i => i.name); | |
console.log(fullProps); | |
var values = fullProps.map((pro) => { | |
return splitted[pro.position]; | |
}) | |
clas.constructor.apply(clas, values); | |
obj[campo.name] = {}; | |
clas = _.pick(clas, propsToGet); | |
Object.assign(obj[campo.name], clas); | |
break; | |
default: | |
break; | |
} | |
acc[indx] = obj; | |
return acc; | |
}, []); | |
console.log(array); | |
} catch (error) { | |
console.log(error); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment