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

feat: add random as a static member of TinyColor #175

Merged
merged 5 commits into from
Aug 3, 2018
Merged

feat: add random as a static member of TinyColor #175

merged 5 commits into from
Aug 3, 2018

Conversation

scttcper
Copy link

@scttcper scttcper commented Aug 2, 2018

No description provided.

@scttcper scttcper mentioned this pull request Aug 2, 2018
@bgrins
Copy link
Owner

bgrins commented Aug 2, 2018

This got to my expected behavior - thanks! But I don't think it's working exactly how I expected it would. What I expected:

  • The default export is 'TinyColor' (not worried about case here), which is a class but also has static methods like random defined via static keyword in typescript. We could also export random, etc as separate methods directly for module consumers.

What I think is currently happening:

  • We are exporting a plain object ('tinycolor' - again the case doesn't matter here), which has methods exposed as keys on the object, along with a class 'TinyColor' assigned in the same way.

The reason I expected the former behavior is that so when we load the UMD package - for example, by doing:

echo '<script src="tinycolor.umd.min.js"></script>' > dist/bundles/index.html && open dist/bundles/index.html

Then in the console in that page, if I do tinycolor.random() it works as expected (matching v1 API). If I do new tinycolor("red") or tinycolor("red") it throws errors (since tinycolor is just a plain object. If instead 'TinyColor' was exported, we could do all of these (I think - please correct me if I'm wrong): TinyColor.random(), new TinyColor("red"), TinyColor("red").

@@ -16,7 +15,7 @@ const mostReadableEl = document.querySelector<HTMLElement>('#mostReadable');
const colorBoxEl = document.querySelector<HTMLElement>('#colorBox');

function colorChange(color) {
const tiny = new TinyColor(color);
const tiny = new tinycolor.TinyColor(color);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the other comment first, but do we need to prefix with tinycolor. since TinyColor is exposed on the window in line 5?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically it might work, but typescript won't like it

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I was wondering if that was the case. No problem.

@scttcper
Copy link
Author

scttcper commented Aug 2, 2018

okay i think what we're looking for is something like this then. Typescript sure doesn't like it very much. I'd never done an export like this before, learn something new every day.

This was my small test.

class Test {
  constructor() { console.log('created test') }
  go() { console.log('go') }
}

var _old = Test;
Test = function(...args) { return new _old(...args) };

// can be called either way
Test().go()
new Test().go()

@codecov-io
Copy link

codecov-io commented Aug 2, 2018

Codecov Report

Merging #175 into v2 will increase coverage by 0.16%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##              v2     #175      +/-   ##
=========================================
+ Coverage   98.4%   98.56%   +0.16%     
=========================================
  Files         10       10              
  Lines        626      628       +2     
  Branches     150      150              
=========================================
+ Hits         616      619       +3     
+ Misses        10        9       -1
Impacted Files Coverage Δ
src/index.ts 97.73% <100%> (+0.02%) ⬆️
src/random.ts 97.27% <0%> (+0.9%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 96aeff9...6500026. Read the comment docs.

@bgrins
Copy link
Owner

bgrins commented Aug 2, 2018

Yeah TS doesn't seem too happy about it. I think it's nice to have the backwards-compat syntax without new, but I think it'd also be OK to drop the non-new format and just export the class if we tack on random, etc as statics. As far as doing that - it seems like that's relatively straightforward as static random = random; (assuming that random it imported at the top of index.ts), right?

@bgrins
Copy link
Owner

bgrins commented Aug 2, 2018

but I think it'd also be OK to drop the non-new format and just export the class if we tack on random, etc as statics.

Specifically, supporting:

TinyColor.random();
new TinyColor("red");

@scttcper
Copy link
Author

scttcper commented Aug 3, 2018

Revered the optional new tinycolor and added random as a member of the TinyColor class.

@scttcper scttcper changed the title docs: expose tinycolor.random in demo feat: add random as a static member of TinyColor Aug 3, 2018
@scttcper scttcper merged commit 01caecf into bgrins:v2 Aug 3, 2018
@bgrins
Copy link
Owner

bgrins commented Aug 3, 2018

Great, thanks! Could you also expose readability, fromRatio, legacyRandom, toMsFilter` in the same manner? This would allow us to remove most of the "several functions" section in the migration guide at https://github.com/bgrins/TinyColor/blob/01caecf94411443d020310dbd724cde38669f13d/README.md#changes-from-v1.

@@ -41,6 +42,7 @@ export class TinyColor {
gradientType?: string;
/** rounded alpha */
roundA!: number;
random = random;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking this would be a static, so you could do TinyColor.random() instead of new TinyColor.random(). This is basically to support the v1 API (as in https://github.com/bgrins/TinyColor#random)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at TS docs, I think this would be done as:

static random = random;

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

Successfully merging this pull request may close these issues.

3 participants