Giter Site home page Giter Site logo

Comments (12)

mvayngrib avatar mvayngrib commented on May 31, 2024

+1

from typeforce.

dcousens avatar dcousens commented on May 31, 2024

Interesting, is this using uglifyjs? If so, I'll see if I can replicate a test case ASAP 👍

from typeforce.

mvayngrib avatar mvayngrib commented on May 31, 2024

@dcousens were you able to replicate the issue?

from typeforce.

dcousens avatar dcousens commented on May 31, 2024

@mvayngrib sorry, I am not able to replicate this issue.
I've uglified the library using uglifyjs and then tested importing it, with no issues.

Is there anything else I need to do?

from typeforce.

mvayngrib avatar mvayngrib commented on May 31, 2024

@dcousens sorry for the radio silence. Here's an example that breaks for me

// fnname.js
var typeforce = require('typeforce')

function Blah () {}

var blah = new Blah()
typeforce('Blah', blah)
browserify fnname.js | uglifyjs -c -m > fnname.min.js && node fnname.min.js
(function (exports, require, module, __filename, __dirname) { !function t(r,e,n){function i(u,a){if(!e[u]){if(!r[u]){var f="function"==typeof require&&require;if(!a&&f)return f(u,!0);if(o)return o(u,!0);var s=new Error("Cannot find module '"+u+"'");throw s.code="MODULE_NOT_FOUND",s}var h=e[u]={exports:{}};r[u][0].call(h.exports,function(t){var e=r[u][1][t];return i(e?e:t)},h,h.exports,t,r,e,n)}return e[u].exports}for(var o="function"==typeof require&&require,u=0;u<n.length;u++)i(n[u]);return i}({1:[function(t,r,e){function n(){}var i=t("./"),o=new n;i("Blah",o)},{"./":2}],2:[function(t,r,e){(function(t){function e(t){if(void 0===t)return"";if(null===t)return"";var r=t.constructor.toString().match(/function (.*?)\(/);return r?r[1]:null}function n(r,n){r.split("|").forEach(function(i){switch(i){case"Array":if(null!==n&&void 0!==n&&n.constructor===Array)return;break;case"Boolean":if("boolean"==typeof n)return;break;case"Buffer":if(t.isBuffer(n))return;break;cas

TypeError: Expected Blah, got n [object Object]
    at o (/Users/tenaciousmv/Code/typeforce/fnname.min.js:1:1468)
    at /Users/tenaciousmv/Code/typeforce/fnname.min.js:1:1170
    at Array.forEach (native)
    at Object.n [as string] (/Users/tenaciousmv/Code/typeforce/fnname.min.js:1:775)
    at r.exports (/Users/tenaciousmv/Code/typeforce/fnname.min.js:1:1703)
    at Object.a.1../ (/Users/tenaciousmv/Code/typeforce/fnname.min.js:1:557)
    at i (/Users/tenaciousmv/Code/typeforce/fnname.min.js:1:316)
    at t (/Users/tenaciousmv/Code/typeforce/fnname.min.js:1:483)
    at Object.<anonymous> (/Users/tenaciousmv/Code/typeforce/fnname.min.js:1:500)
    at Module._compile (module.js:435:26)

from typeforce.

dcousens avatar dcousens commented on May 31, 2024

@mvayngrib right, that is simply a risk of uglify.
Due to it mangling the name Blah.

This is an underlying issue with using quacksLike, if you mangle the names, its inherently risky.

You're best bet is to exclude Blah from being mangled in uglify-js using:

uglifyjs --reserved-names Blah

Alternatively, you can make your own type function (with all of its inherent risks):

function BlahType (value) { return value instanceof Blah }
typeforce(BlahType, blah)

or

typeforce(v => v instanceof Blah, blah)

I might make a typeforce.instanceOf(...) type function so you can do typeforce(typeforce.instanceOf(Blah), blah) to make this a bit simpler.

from typeforce.

dcousens avatar dcousens commented on May 31, 2024

Alternate alternatively: typeforce(Blah.name, blah) might work.

(Blah.toString().match(/function (.*?)\s*\(/)[1] if name is undefined).

from typeforce.

mvayngrib avatar mvayngrib commented on May 31, 2024

right, that's more or less what i've been doing. It is what it is :)

from typeforce.

dcousens avatar dcousens commented on May 31, 2024

I'm going to close this as wontfix, but it isnt' because I don't want to fix it, I just don't think we can due to the nature of it.

Maybe we should put a warning in the README?

from typeforce.

mvayngrib avatar mvayngrib commented on May 31, 2024

@dcousens yes to wontfix, yes to warning. Btw, i noticed this issue when I was uglifying bitcoinjs-lib, an older version, but i think the latest one uses quacksLike (which as you say has the same issue)

from typeforce.

dcousens avatar dcousens commented on May 31, 2024

@mvayngrib ah! You probably should have noticed the warning that is already in the bitcoinjs-lib README then 😄

from typeforce.

mvayngrib avatar mvayngrib commented on May 31, 2024

@dcousens haha, you got me, totally missed that

from typeforce.

Related Issues (20)

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.