Tags Validation | Typescript and Python | Hivemind

in Steem Dev10 months ago

Hello developers, I have been reading and comprehending the Python code of the Hivemind. I noticed that there are many reusable functions. Therefore, in this Hivemind series, I will attempt to share the useful functions provided by Hivemind in Python and convert them into Typescript. This conversion will enhance type handling and make them usable in Typescript or Javascript.

updates.png

Today, we will explore Hivemind's validation of input tags and its implementation in Python. Additionally, we will convert it into Typescript. Let's delve into it.

GitHub

https://github.com/steemit/hivemind

Python

import re

def valid_tag(tag:str, allow_empty=False):
    """Returns validated tags or throws Assert."""
    if not tag:
        assert allow_empty, 'tag was blank'
        return ""
    assert isinstance(tag, str), 'tag must be a string'
    assert re.match('^[a-z0-9-_]+$', tag), 'invalid tag `%s`' % tag
    return tag
    
def valid_tags(tags:[], allow_empty=False):
    """Returns validated tag or throws Assert."""
    if not tags:
        assert allow_empty, 'empty tags'
        return []
    for tag in tags:
        assert isinstance(tag, str), 'tag must be a string'
        assert re.match('^[a-z0-9-_]+$', tag), 'invalid tag `%s`' % tag
    return tags

Example (Python)

print(valid_tag('steempro'))

print(valid_tags(['steempro','steemit']))


Typescript

function valid_tag(tag: string, allowEmpty: boolean = false): string {
    const regex = /^[a-z0-9-_]+$/;

    if (!tag) {
        if (!allowEmpty) {
            throw new Error('Empty tag');
        }
        return '';
    }

    if (typeof tag !== 'string') {
        throw new Error('Tag must be a string');
    }

    if (!regex.test(tag)) {
        throw new Error(`Invalid tag \`${tag}\``);
    }
    

    return tag;
}
function valid_tags(tags: string[], allowEmpty: boolean = false): string[] {
    const regex = /^[a-z0-9-_]+$/;

    if (!tags) {
        if (!allowEmpty) {
            throw new Error('Empty tags');
        }
        return [];
    }

    for (const tag of tags) {
        if (typeof tag !== 'string') {
            throw new Error('Tag must be a string');
        }

        if (!regex.test(tag)) {
            throw new Error(`Invalid tag \`${tag}\``);
        }
    }

    return tags;
}

Example (Typescript)

console.log(valid_tag('steempro'))

console.log(valid_tags(['steempro','steemit']))



Best Regards @faisalamin

Download SteemPro Mobile

Sort:  
 10 months ago 

It is very gratifying when you explore hivemind. I also spent some time reading the code.... and saw some amazing things.
Have you actually considered helping to develop the code and make some improvements? I'd really like to tweak some, bring it up to date and improve it. See also the suggestion I made here.
Do you think improvements are possible and desirable at all?

I'm glad you found exploring Hivemind gratifying. It's great that you're interested in improving the code. Improvements are indeed possible and desirable to keep the project up to date and enhance its functionality. Me too exploring hivemind for the betterment.

Also extracting useful functionalities. You contributions and projects are great.

Posted using SteemPro Mobile

 10 months ago 

You have also a lot to do with the SteemPro project.
Thank you for your witness vote.

My pleasure, I am also running the witness node. ranked 71

 9 months ago 

I know and you got my vote decades ago ;-))

Coin Marketplace

STEEM 0.16
TRX 0.12
JST 0.026
BTC 57110.78
ETH 2521.26
USDT 1.00
SBD 2.32