advanced address parsing

This commit is contained in:
muerwre 2019-03-20 14:15:08 +07:00
parent ea08ab8a5e
commit 89a13f1475
5 changed files with 82 additions and 19 deletions

View file

@ -1,6 +1,6 @@
const { User, Route } = require('../../models');
const { parseRoute, parseStickers, parseString, parseNumber } = require('../../utils/parse');
const { parseRoute, parseStickers, parseString, parseNumber, parseAddress } = require('../../utils/parse');
module.exports = async (req, res) => {
const { body, body: { id, token, force } } = req;
@ -9,7 +9,7 @@ module.exports = async (req, res) => {
if (!owner) return res.send({ success: false, reason: 'unauthorized', id, token });
const title = parseString(body.title, 64);
const address = parseString(body.address, 32);
const address = parseAddress(body.address, 32)
const route = parseRoute(body.route);
const stickers = parseStickers(body.stickers);
const logo = parseString(body.logo, 16);

View file

@ -21,5 +21,10 @@ module.exports.parseStickers = stickers => stickers.filter(el => (
));
// .map(el => ((el.text && String(el.text).substr(0, 100)) || ''));
module.exports.parseString = (value, size) => (value && String(value).substr(0, size)) || '';
const parseString = (value, size) => (value && String(value).substr(0, size)) || '';
module.exports.parseNumber = (value, min, max) => (value && Number(value) && Math.min(max, Math.max(min, value))) || 0;
module.exports.parseString = parseString;
module.exports.parseAddress = (value, size) => (
parseString(value, size).replace(/[^A-Za-z\-_0-9]/ig, '_').replace(/_{2,}/ig, '_')
);