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

Stringify object using Object.entries instead of separated function #418

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { extractCss } from './core/update';
export { styled, setup } from './styled';
export { css, glob, keyframes } from './css';
Comment on lines +1 to +3
Copy link
Owner

Choose a reason for hiding this comment

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

Oh this is generated by microbundle but I've added a flag to not get this anymore. Can you check that you have this line in your fork? https://github.com/cristianbote/goober/blob/master/package.json#L75

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I have the same package.json, but this file is still being created

36 changes: 18 additions & 18 deletions src/__tests__/integrations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,17 @@ describe('integrations', () => {
'"',
' ', // Empty white space that holds the textNode that the styles are appended
'@keyframes go384228713{0%{opacity:0;}99%{opacity:1;color:dodgerblue;}}',
'.go1127809067{opacity:0;background:cyan;}',
'.go3244392539{opacity:0;background:cyan;}',
'.go3865451590{color:red;}',
'.go3991234422{color:cyan;}',
'.go3991234422 .go3865451590{border:1px solid red;}',
'.go1925576363{color:blue;}',
'.go3206651468{color:green;}',
'.go4276997079{color:orange;}',
'.go2069586824{opacity:0;animation:go384228713 500ms ease-in-out;}',
'.go631307347{foo:1;color:red;baz:0;}',
'.go3865943372{opacity:0;}',
'.go1162430001{opacity:0;baz:0;}',
'.go1162043959{foo:1;color:red;baz:0;}',
'.go98589992{opacity:0;}',
'.go3678641885{opacity:0;baz:0;}',
'"'
].join('')
);
Expand Down Expand Up @@ -162,21 +162,21 @@ describe('integrations', () => {
expect(target.innerHTML).toEqual(
[
'<div>',
'<div class="go103173764"></div>',
'<div class="go103194166"></div>',
'<span class="go2081835032"></span>',
'<button class="go1969245729 go1824201605"></button>',
'<div class="go2623058580"></div>',
'<div class="go2625119182"></div>',
'<span class="go2458456096"></span>',
'<button class="go63716261 go1824201605"></button>',
'</div>'
].join('')
);

expect(extractCss()).toMatchInlineSnapshot(
[
'"',
'.go1969245729{color:white;padding:0em;margin:1em;}',
'.go103173764{color:white;padding:0em;}',
'.go103194166{color:white;padding:2em;}',
'.go2081835032{color:white;padding:3em;margin:1em;}',
'.go63716261{color:white;padding:0em;margin:1em;}',
'.go2623058580{color:white;padding:0em;}',
'.go2625119182{color:white;padding:2em;}',
'.go2458456096{color:white;padding:3em;margin:1em;}',
'.go1824201605{background:dodgerblue;}',
'"'
].join('')
Expand Down Expand Up @@ -216,9 +216,9 @@ describe('integrations', () => {
expect(target.innerHTML).toEqual(
[
'<div>',
'<div class="go103173764"></div>',
'<div class="go103194166"></div>',
'<span class="go2081835032"></span>',
'<div class="go2623058580"></div>',
'<div class="go2625119182"></div>',
'<span class="go2458456096"></span>',
'</div>'
].join(''),
`"<div><div class=\\"go103173764\\"></div><div class=\\"go103194166\\"></div><span class=\\"go2081835032\\"></span></div>"`
Expand All @@ -227,9 +227,9 @@ describe('integrations', () => {
expect(extractCss()).toMatchInlineSnapshot(
[
'"',
'.go103173764{color:white;padding:0em;}',
'.go103194166{color:white;padding:2em;}',
'.go2081835032{color:white;padding:3em;margin:1em;}',
'.go2623058580{color:white;padding:0em;}',
'.go2625119182{color:white;padding:2em;}',
'.go2458456096{color:white;padding:3em;margin:1em;}',
'"'
].join('')
);
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/styled.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('styled', () => {
className: 'go',
color: 'red'
});
expect(extractCss()).toEqual('.go3433634237{color:red;}');
expect(extractCss()).toEqual('.go2363673541{color:red;}');
});

it('change tag via "as" prop', () => {
Expand Down Expand Up @@ -154,6 +154,6 @@ describe('styled', () => {
let vnode = Tag({ draw: true });

expect(vnode).toMatchVNode('tag', { className: 'go', draw: true });
expect(extractCss()).toEqual('.go2986228274{color:yellow;}');
expect(extractCss()).toEqual('.go2754571358{color:yellow;}');
});
});
6 changes: 3 additions & 3 deletions src/core/__tests__/hash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('hash', () => {

const res = hash({ baz: 1 }, 'target');

expect(toHash).toBeCalledWith('baz1');
expect(toHash).toBeCalledWith('baz,10');
expect(astish).not.toBeCalled();
expect(parse).toBeCalledWith({ baz: 1 }, '.' + className);
expect(update).toBeCalledWith('parse()', 'target', undefined);
Expand All @@ -100,12 +100,12 @@ describe('hash', () => {

// Since it's not yet cached
hash({ cacheObject: 1 }, 'target');
expect(toHash).toBeCalledWith('cacheObject1');
expect(toHash).toBeCalledWith('cacheObject,10');
toHash.mockClear();

// Different object
hash({ foo: 2 }, 'target');
expect(toHash).toBeCalledWith('foo2');
expect(toHash).toBeCalledWith('foo,20');
toHash.mockClear();

// First object should not call .toHash
Expand Down
17 changes: 1 addition & 16 deletions src/core/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ import { parse } from './parse';
*/
let cache = {};

/**
* Stringifies a object structure
* @param {Object} data
* @returns {String}
*/
let stringify = (data) => {
if (typeof data == 'object') {
let out = '';
for (let p in data) out += p + stringify(data[p]);
return out;
} else {
return data;
}
};

/**
* Generates the needed className
* @param {String|Object} compiled
Expand All @@ -34,7 +19,7 @@ let stringify = (data) => {
*/
export let hash = (compiled, sheet, global, append, keyframes) => {
// Get a string representation of the object or the value that is called 'compiled'
let stringifiedCompiled = stringify(compiled);
let stringifiedCompiled = typeof compiled == 'object' ? Object.entries(compiled) + 0 : compiled;

// Retrieve the className from cache or hash it in place
let className =
Expand Down