Giter VIP home page Giter VIP logo

Comments (5)

sindresorhus avatar sindresorhus commented on May 14, 2024 4

I just realized I have a module for this: https://github.com/sindresorhus/fn-args Totally forgot about it.

from ow.

SamVerschueren avatar SamVerschueren commented on May 14, 2024

I'm looking into this, but unless I'm totally missing something, it doesn't feel like fn-args would help here. I'm not even sure if it's possible. The reason is that fn-args (or the solution from StackOverflow) returns the parameter names of the function being invoked. Meaning that you can do fnArgs(ow) which returns ['e', 't'] (dist bundle). But what we want is the name of the variable passed in into the invocation of that function which seems a totally different thing.

from ow.

SamVerschueren avatar SamVerschueren commented on May 14, 2024

This makes me wonder, how does power-assert do this?

from ow.

SamVerschueren avatar SamVerschueren commented on May 14, 2024

I tried using callsites for this

const callsites = require('callsites');
const fs = require('fs');

const ow = function(value, predicate) {
	const site = callsites()[1];

	const line = site.getLineNumber();

	const content = fs.readFileSync(site.getFileName(), 'utf8').split('\n');

	console.log(content[line - 1]);
};

const x = 'foo';
const predicate = {};

ow(x, predicate);

This prints the string ow(x, predicate) where we should be able to strip out x. I was hoping this information was present in the callsite itself, but it isn't. So this solution would only work in Node because it uses fs.readFileSync.

from ow.

sindresorhus avatar sindresorhus commented on May 14, 2024

The above is exactly what we do in AVA too to get a nice output. I think Node.js-only is better than nothing. My main use-case is Node.js anyway. We just have to document it clearly.

from ow.

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.