Coder Social home page Coder Social logo

safeguid's Introduction

SafeGuid

A better typescript guid implementation. The reason for this library is to explicitely inherit string so we can serialize the guid over the wire to/from JSON more safely. Also it has better guid formatting support for the 4 major guid string formats, B, P, D, and N.

Installation

npm i safeguid --save

Basic Usage

import { SafeGuid } from 'safeguid';
import { Guid as _tsGuid } from 'typescript-guid';

export class Example {
    public id: IGuid;
    public guidString: string;

    constructor() {
        // generate a random guid
        id = SafeGuid.newGuid();
        // get the raw string of the guid, "N" format
        guidString = id.valueOf();
        // parse a guid from string
        id = SafeGuid.parse("b77d409a-10cd-4a47-8e94-b0cd0ab50aa1");
        // Default to string implementation, "D" format, the same as toJSON for json serialization
        guidString = id.toString();
        // Empty guid
        id = SafeGuid.EMPTY;
        // Construct from any supported format
        id = new SafeGuid(); // empty
        id = new SafeGuid("b77d409a-10cd-4a47-8e94-b0cd0ab50aa1"); // from a string
        id = new SafeGuid(new SafeGuid("b77d409a-10cd-4a47-8e94-b0cd0ab50aa1")); // from another IGuid
        id = new SafeGuid(_tsGuid.parse("b77d409a-10cd-4a47-8e94-b0cd0ab50aa1")); // interop support for typescript-guid
    }
}

safeguid's People

Contributors

slawlor avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

mchlroy

safeguid's Issues

Modify createMap method return type

public static createMap<T>(entries?: Iterable<[IGuid | string, T]> | null) : Map<IGuid,T> {

I think the return type should be GuidMap<T>, so that we can call GuidMap<T>.get( Guid | string ).
In fact, Map<IGuid,T>.get method signature only accepts a string as parameter.
Otherwise, we need to manually cast the returned object to GuidMap<T> in order to be able to pass a string to the get method.

Some base64 strings returns true when passed in isGuid

We had a bug in our application where we wrongly tried to convert a base64 string into a SafeGuid, but we did do a check with SafeGuid.isGuid(input) before hand.

The thing is, SafeGuid.isGuid returned true for a base64 strings.

I have shrunk the base64 as most as I can for it to still return true, here is that string:

SafeGuid.isGuid("iVBORw0KGgoAAAANSUhEUgAAAtAAAAPACAIAAACTlNnkAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhezP1pk2VJcqaJ+b57RlajIMN/wiGHFJIy/MLfMjPd2BpooNGNpae7MT3CGbYIfyJFBgSqsjIzIjx8dz6qjx29es+554ZHFUjOW5UWaqqvLmbnXDO7qx/+7Z//yeuEt7e3g8Th4eHR0ZHyDHAABHB8fEyrHtkgdhGeE5cX14QqaAX39/e6g/Pz87Ozsy9fvvz8888I0DDRGpxQZHx6esrwEV8ryjAdRkkILy8v9GhfskZiBu3wJBIcH52enl5eXqJ8e31GfzKhchkQKJMlQr28PD4+0DIQWpQRLat6eHiIwMfHFExw3YP5eC+HbtYbBUf3ZWt6AS60dAlycXFheWhy2p6JH9VMQ2P4j4+P6OEbB3fyOgS8rq+vrROckjFNf/1AfVHSJBYWk3AQIz/ZWPQF0DzUxVbtDAE7sa47ql1e4BokFcZHP6JYeV9126Qcp64Rz3pNH6eOXZJHQ5cGsq5yiN/jBEAJJYl6SDAnEyGhoUycChIGMuIyy2gz10379INFimQttbe22HqYQC5VLAtGw6qVGUWM5RchRoCbRqiV+dfKogc0dyy5pmRlgpusqJgVOOFc5Cu0ZjxyRcU6LhaS+RmiDp0sRcorqU64dF+brg1ZCQAJUKZVUGnmtOO/bS8cK/LC2SuzjnCTLYgR4e6eeWlEk44Q8AWLKUg+zjIuCCiS06u8q+k0TsRMAKcUv18kLUxrAmV11dsRnXwpno");
// Returns true

It seems like a substring matches the regex:
image

So it looks like the regex could be improved. I think we should encapsulate both regex in either side of the '|' with ^ and $ so that it does not allow longer strings. And also only allow a-f instead of a-z.

^(?{?(([a-z0-9]{8})-([a-z0-9]{4})-([a-z0-9]{4})-([a-z0-9]{4})-([a-z0-9]{12})))?}?|(([a-z0-9]{8})([a-z0-9]{4})([a-z0-9]{4})([a-z0-9]{4})([a-z0-9]{12}))$
-->
^(?{?(([a-f0-9]{8})-([a-z0-9]{4})-([a-z0-9]{4})-([a-z0-9]{4})-([a-z0-9]{12})))?}?$|^(([a-z0-9]{8})([a-z0-9]{4})([a-z0-9]{4})([a-z0-9]{4})([a-z0-9]{12}))$```

I'll open a PR if that's alright!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.