npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

type-infer

v0.1.2

Published

Infer data types with specifity. A String type can be further differentiated into TEXT, TINYTEXT and so on. Built to be compatible with MYSQL Data Types!

Downloads

7

Readme

Why?

Type inference is important when you are dealing with arbitrary data. It is however even more important when toy want to use that data in a "type-safe" manner like adding into database tables with specific, strict schema.

This module tries it's best to not only infer datatype but also add important metadata to indicate type specificity. For example, Strings can also be "CHAR", "TINYTEXT", "TEXT" specific. Dates can be "DATE" or "DATETIME" specific.

This module tries to model types against MYSQL Data Types as indicated Here.

Usage

Install yarn add type-infer then:


    //This example checks different datatypes via a loop
    //See test.js for more...

    const type = require('type-infer');

    let opts = {
        parseTime: true,
        parseDate: true,
        parseNumber: true
    };
    
    
    let vals = [
        // Date with time
        new Date().toISOString(),
        // Date alone
        '2021-03-31',
        // Time
        '12:34', '13:55', '1:23 pm',
        // strings
        "this is a string", "a", randomStr(200), randomStr(700),
        // integers
        18890, "43564", -23, '-3673462',
        // big integers
        '9223372036854775808', '-9223372036854775808',
        // floats
        322.34, "332.78", '9223372036854775808.889',
        //booleans
        true, false
    
    ]
    
    let resp = vals.map(val => type(val, opts));
    
    console.log(JSON.stringify(resp, 0, 4));

This will log the following JSON:


    [
      {
        value: '2021-07-23T07:50:10.858Z',
        type: 'DATE',
        specific: 'DATETIME'
      },
      { value: '2021-03-31', type: 'DATE', specific: 'DATE' },
      { value: '12:34', type: 'TIME', specific: '24HR' },
      { value: '13:55', type: 'TIME', specific: '24HR' },
      { value: '1:23 pm', type: 'TIME', specific: '12HR' },
      { value: 'this is a string', type: 'STRING', specific: 'TINYTEXT' },
      { value: 'a', type: 'STRING', specific: 'CHAR' },
      {
        value: 'dKrmgnsa23BzNoloEK07fcTO1YUtqZm8phkrbjrE4RAAc3lRuDhjYDAdTh5XnvHhLGzyFzxsNLTYab0p4e4lUpcHo5DU420fgEAvFAE9fLhOSPwEv9cDPoYoQ2ZMPXZ65ykx0Am8EdDp5OD1Ya9z5doQU87rDjhIUVG1blU4K1DOR2jGgiwiACWnygNWfLYJX8M8kF57',
        type: 'STRING',
        specific: 'TINYTEXT'
      },
      {
        value: 'iphxL9jfWUlrUKIq8S2nUluMkRrVBT1mhZq2OgHqVE0fRpslnXRNaBbzikKICEgn94BL60CkGHtRufDFiG6XoqgRYZqvLCGS0mY0RjE7esBsLLKbbxiItcuodwlsV4SMkmyPXQ4pJmq6EhiT3zFPQOY7BaKEHck4Q2z7VkoIP11iWaxWvLtSrbfLRWNXhgY7gUP6qFRVA9EzbTbkFpVyYguNcLjQTB8VCM3HY7Y3CtPxUbTdV2NrydVWTigkismLVUEpBHswGMu1RhJ4AP5qq89b1dHem9L0ZVo3P8EI31lUogqp9YPicWuumZ7xGsqhsPKklRoTjyQEnqs0pcIG0PWdJJTwx9wrOCjN7vWbifBiuuFY3PqptEwBFIzQZ2QSbd2iZjgm3v7wdOmq94nfbRXY8nyeVtWD7kzByEdFcMBQqtuti8pxVo1Z3FGj5jbL7bNJtzdfTADPTOz5nN8W4ffTNSbcmX3qANTzsIjOyFIXuzwPYV7ZKJC4UK0zd0zXqJwDAxc6WI90SXaqg3rDRyu05jeCrIoOMaL2k9p6fACyGW6Sk7kfRPkMqr8887zzoKMwGbNIxnjwq0GwC0aeoUFpjzDgEwgrpbgiwBOH5ZkaJSZ2EvwTRNuzTEL8dcUNMkvOyhjE6p80SkdWfpLJuq9TrpgLXgnnpZcWlphjpC8tgSRs496KTZg56nQu',
        type: 'STRING',
        specific: 'TEXT'
      },
      { value: 18890, type: 'NUMBER', specific: 'INT' },
      { value: 43564, type: 'NUMBER', specific: 'INT' },
      { value: -23, type: 'NUMBER', specific: 'INT' },
      { value: -3673462, type: 'NUMBER', specific: 'INT' },
      { value: '9223372036854775808', type: 'NUMBER', specific: 'BIGINT' },
      { value: '-9223372036854775808', type: 'NUMBER', specific: 'BIGINT' },
      { value: 322.34, type: 'NUMBER', specific: 'FLOAT' },
      { value: 332.78, type: 'NUMBER', specific: 'FLOAT' },
      {
        value: '9223372036854775808.889',
        type: 'NUMBER',
        specific: '*FLOAT'
      },
      { value: true, type: 'BOOLEAN', specific: 'NONE' },
      { value: false, type: 'BOOLEAN', specific: 'NONE' }
    ]    

Note:

  • The specific types with an asterisk (*) such as *FLOAT. This means that the float is based on a number that Javascipt cannot guarantee precision of. Read More

  • Also note that with BIGINT and *FLOAT types return a stringified version of the value in a bid to try and maintain precision.

  • Below is the helper function for the example above.


    function randomStr(length) {
        var result = '';
        var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        var charactersLength = characters.length;
        for (var i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() *
                charactersLength));
        }
        return result;
    }