-
-
Save sergiolepore/f623e4627f78d1a4693b to your computer and use it in GitHub Desktop.
2012-10-18-como-construir-un-modulo-de-nodejs-e-integrarlo-con-npm.markdown
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
var lib = require('./mi_libreria.js'); |
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
var lib = require('./mi_liberia'); |
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
module.exports = require('./lib/param_replacer'); |
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
var param_replacer = require('./lib/param_replacer'); | |
var objetivo = "%hello% %world%! -- %world% %hello%!"; | |
var idioma = "es"; | |
var reemplazos = { | |
"en": { | |
"hello": "Hello", | |
"world": "World" | |
}, | |
"es": { | |
"hello": "Hola", | |
"world": "Mundo" | |
} | |
}; | |
var resultado = param_replacer.replace(objetivo, reemplazos[idioma]); | |
console.log(resultado); |
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
var replacer = require('./param_replacer'); |
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
{ | |
"name": "param_replacer", | |
"description": "Node.js Parameter Replacer", | |
"version": "0.1.0", | |
"author": "Sergio Lepore <mail@example.net>", | |
"maintainers": [ | |
"sergiolepore <mail@example.net>" | |
], | |
"repository": { | |
"type": "git", | |
"url": "https://github.com/sergiolepore/param_replacer.git" | |
}, | |
"keywords": [ | |
"parmeters", | |
"replacer" | |
], | |
"main": "./lib/param_replacer", | |
"license": "BSD-3", | |
"engines": { | |
"node": ">= 0.6" | |
} | |
} |
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
var reemplazo = "Hello", | |
entrada = "%param% World!"; | |
var salida = entrada.replace(/%(.*?)%/g, reemplazo); | |
console.log(salida); |
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
var reemplazos = { | |
"hello": "Hello", | |
"world": "World" | |
}; | |
var objetivo = "%hello% %world%! -- %world% %hello%!", | |
param_encontrados = objetivo.match(/%(.*?)%/g); | |
// Tener en cuenta que usamos match. Este método devuelve | |
// un array de coincidencias, pero si no encuentra nada | |
// devuelve null. | |
if (param_encontrados) { | |
var nombre_param = null, | |
valor_reemplazo = null; | |
for (var i=0; i < param_encontrados.length; i++) { | |
// Quitamos los % del parámetro. | |
nombre_param = param_encontrados[i].replace(/%/g, ''); | |
valor_reemplazo = reemplazos[nombre_param]; | |
// Vamos reemplazando los parámetros con sus valores | |
// de forma iterativa. | |
objetivo = objetivo.replace(param_encontrados[i], valor_reemplazo); | |
} | |
} | |
console.log(objetivo); |
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
var reemplazos = { | |
"en": { | |
"hello": "Hello", | |
"world": "World" | |
}, | |
"es": { | |
"hello": "Hola", | |
"world": "Mundo" | |
} | |
}; | |
var idioma = "es"; | |
var objetivo = "%hello% %world%! -- %world% %hello%!", | |
param_encontrados = objetivo.match(/%(.*?)%/g); | |
// Tener en cuenta que usamos match. Este método devuelve | |
// un array de coincidencias, pero si no encuentra nada | |
// devuelve null. | |
if (param_encontrados) { | |
var nombre_param = null, | |
valor_reemplazo = null; | |
for (var i=0; i<param_encontrados.length; i++) { | |
// Quitamos los % del parámetro. | |
nombre_param = param_encontrados[i].replace(/%/g, ''); | |
// Añadimos el soporte de idiomas :) | |
valor_reemplazo = reemplazos[idioma][nombre_param]; | |
// Vamos reemplazando los parámetros con sus valores | |
// de forma iterativa. | |
objetivo = objetivo.replace(param_encontrados[i], valor_reemplazo); | |
} | |
} | |
console.log(objetivo); |
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
exports.replace = function(objetivo, reemplazos) { | |
var param_encontrados = objetivo.match(/%(.*?)%/g); | |
if (param_encontrados) { | |
var nombre_param = null, | |
valor_reemplazo = null; | |
for (var i=0; i<param_encontrados.length; i++) { | |
nombre_param = param_encontrados[i].replace(/%/g, ''); | |
valor_reemplazo = reemplazos[nombre_param]; | |
objetivo = objetivo.replace(param_encontrados[i], valor_reemplazo); | |
} | |
} | |
return objetivo; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment