VerbalExpressions / JSVerbalExpressions

JavaScript Regular expressions made easy
http://verbalexpressions.github.io/JSVerbalExpressions/
MIT License
12.17k stars 504 forks source link

TypeError: VerEx.sanitize is not a function #175

Closed amoshydra closed 5 years ago

amoshydra commented 6 years ago

Current Problem

sanitize is not available as a static function as documented in here https://verbalexpressions.github.io/JSVerbalExpressions/VerbalExpression/utilities

VerEx.sanitize is undefined

Reproduce link: https://codesandbox.io/s/0437l1r9jl

Expected behaviour

I was expected to use VerEx sanitize function like this

const filePath = '/path/to/file'
const sanitized = VerEx.sanitize(filePath)
sanitized.source === ///path//to//file/.source

Environment

Version tested:

shreyasminocha commented 6 years ago

This is because the sanitize method is a static method of the VerbalExpression class. However, we are export only an instance of VerbalExpression (per this). Not sure what the way forward is.

amoshydra commented 6 years ago

Noted.


Would an implementation like this make sense?

function VerEx() { // eslint-disable-line no-unused-vars
    return new VerbalExpression();
}
VerEx.sanitize = VerbalExpression.sanitize;

Related: https://github.com/shreyasminocha/JSVerbalExpressions/blob/5ca909d1/VerbalExpressions.js#L482-L484

shreyasminocha commented 6 years ago

If there'x enough use-case, LGTM; @jehna, @ionutvmi?

jehna commented 6 years ago

Hmm. The documentation actually points to VerbalExpressions.sanitize (there's a typo, it should be VerbalExpression.sanitize). At the moment we don't expose the main VerbalExpression class at all to the public.

I checked the previous versions (0.2.0, 0.2.1 and 0.1.2), and seems that we haven't exposed the main class at any point 🤔

Edit: Side note: So it kinda does work as intended, but you'll need to dig up the hidden VerbalExpression class by doing something like: VerEx().constructor.sanitize

What did work in 0.2.x however was the format VerEx().sanitize(input).

I'd propose that we'd think this from the public API's standpoint: I'd think the most logical way would be to use:

import { sanitize } from 'verbal-expressions'

sanitize(input)

We could then use the es import/exports to export the sanitize function:

export function sanitize(...) {...}

...and use the function instead of the static VerbalExpression.sanitize in the code. How does this sound?

shreyasminocha commented 6 years ago

@jehna Yeah, I noticed the typo too (will open a PR).

Sounds good to me, but it's just that es import/export are annoying to transpile. Even with babel-plugin-transform-es2015-modules-umd, there's the issue of having to add .default at the end of the exported global when using the transpiled version. babel-plugin-add-module-exports doesn't work with anything except CommonJS. I might create my own babel plugin to fix this, but till then we'll have to do it the ES5 way.

jehna commented 6 years ago

@shreyasminocha alright, using VerEx.sanitize = VerbalExpression.sanitize; sounds good 🙂👍 Simple and works!

shreyasminocha commented 5 years ago

181 fixes this.