Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Object.prototype.toString.apply Type #155

Open
nyteshade opened this issue Oct 2, 2017 · 12 comments
Open

Feature Request: Object.prototype.toString.apply Type #155

nyteshade opened this issue Oct 2, 2017 · 12 comments

Comments

@nyteshade
Copy link

For those who are working with things like [Symbol.toStringTag] or those who have previously and often used things like Object.prototype.toString.apply() an ability to get that value easily would also be nice

// Map %C, open to other values, to the "Class" of an object
const classOf = o => Object.prototype.toString.call(o).replace(/\[object (.*?)\]/, '$1')

classOf(/asdfads/) // 'RegExp'
classOf(34) // 'Number'
classOf("asdfasd") // 'String'
classOf(Object.prototype) // 'Object'
classOf(null) // 'Null'
classOf(undefined) // 'Undefined'

let o = {}
o[Symbol.toStringTag] = "My Very Special Object"
classOf(o) // 'My Very Special Object'

Use of the above for a %C sprintf would be like the following

sprintf('%C', /asdfasd/); // 'RegExp'
sprintf('This object is a %C', "asdfas"); // 'This object is a String'
// etc...
@nyteshade
Copy link
Author

Pull request and unit tests submitted.
#156

@alexei
Copy link
Owner

alexei commented Oct 4, 2017

Is %T not working for you?

@alexei
Copy link
Owner

alexei commented Oct 4, 2017

Granted, %T doesn't preserve the case (perhaps erroneously)

@nyteshade
Copy link
Author

nyteshade commented Oct 4, 2017 via email

@alexei
Copy link
Owner

alexei commented Oct 4, 2017

@alexei
Copy link
Owner

alexei commented Oct 4, 2017

NB: I edited your comment to strip my original message and your signature (which contains your email address and phone numbers)

@nyteshade
Copy link
Author

nyteshade commented Oct 4, 2017 via email

@alexei
Copy link
Owner

alexei commented Oct 4, 2017

I was indeed thinking this morning to leave the type as is and break backwards compatibility

@nyteshade
Copy link
Author

nyteshade commented Oct 4, 2017 via email

@alexei
Copy link
Owner

alexei commented Oct 4, 2017

If you do, please allow functions and classes to be converted to type rather than being executed

I suppose you're talking about https://github.com/alexei/sprintf.js/pull/156/files#diff-13ed28d46a5f76f4d44561850bda81bbR61

@nyteshade
Copy link
Author

nyteshade commented Oct 4, 2017 via email

@tshinnic
Copy link

tshinnic commented Jan 30, 2021

Regarding #155 (comment) above:

Executing randomly submitted function values is a . . . hindrance to usage. If users cannot guarantee every value presented to sprintf-js is not tainted by being function reference, random exceptions happen.

I've been having to swap back to console.log repeatedly.

Oh dear, 7 years ago? (d84118b) We're stuck? Or can it be acknowledged as a mis-feature? Was there an obvious use case I'm ignorant of?

.

(edit) Your last comment in #157 mentions the topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants