Do not store file outside of WSL, it is slow

For some reason, I have switch back to Windows OS after years of working on MacOS.Most of the time I'm working on frontend projects, so to get better DX, I use Windows Subsystem for Linux (WSL for short). And I'm using version 2 of it.But I'm not a fancy Vim programmer, I heavily relay on IDEs like VS Code and WebStorm. So I still have to install these IDEs on the Windows.I'm able to install zsh, oh-my-zsh, fuck, p10k along with other tools in the WSL, gives me a similar experience as on Mac.At the first I put the project on Windows file system, because in this way I can easily access the file from IDE. And then in the WSL2 terminal, I go to the Windows path like this:```shell cd /mnt/DISK/PROJECT_FOLDER ```I can run all these frontend commands like `yarn`, `nvm`. Seems smooth.Soon I find a big issue. The hot module replacement not working, the building process is very slow. I have to restart the project dev command after making change to the code. Clearly this is not an acceptable solution.After searching a bit on the internet, I move the project back to WSL file system, then every thing works perfectly. The building is faster, the HMR is working.To access the file in WSL from IDEs, VS Code has [extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-wsl) to open file from WSL easily, just install it, and in the terminal, just switch to the project folder and type `code .`. Or in the IDE, click `File-Open Folder`, it will show your linux home folder by default. For WebStorm, it will also show the WSL folder in the open file dialog.Finally, I can use my i7+32G Windows, don't need to listen to the crazy fan noise on the MBP :D

Read More

A wrong solution on Leetcode

With all these years as an engineer, I never tried Leetcode.Maybe it's because I never prepared for interview before.Well, now I have to, because the company I just joined in 6 month ago, is gonna shutdown it's office here.Technically not shutdown, it just lays off every programmer because it thinks we are more expensive than those in another country.So people all say you have to try Leetcode to practice your programming skill.As a frontend engineer, why should I try it? Most of the time, I'm building UI, calling API, fighting with webpack configuration, cursing Google Lighthouse always gives my webpage low score, arguing with designer why is same font size seems so big on production than on Figma...After I tried to solve some problems on it, I found it is pretty fun, except it made me think myself not a qualified programmer.For the problem [palindrome permutation](https://leetcode.com/problems/palindrome-permutation/). I found it's top 1 solution is not correct.The code is like this:```javascript var canPermutePalindrome = function (s) { let arr = [...new Set(s.split(''))] let m = arr.length - Math.ceil(s.length / 2) if (arr.length == 1 || m == -1 || m == 0) return true return false } ```The code is very short and clean, pretty impressive at the first glance, and most of the cases it works, Leetcode also thinks it's a correct one. But if you try with a string `aaaabbbbcc`, it returns `false`, while it should be `true`. `ababccbaba` clearly is a palindrome. Clearly the test cases could not cover all the scenes Here is my solution, a simple and basic one:```javascript var canPermutePalindrome = function (s) { let set = new Set() for (const str of s) { set.has(str) ? set.delete(str) : set.add(str) } return set.size

Read More

how to skip git hooks when committing

Well, it's not very recommended skipping git hooks. These hooks are often for formatting code, running tests, etc.But life happens, sometimes you want to commit something quickly or even an empty commit to the server so that you can trigger some CI/CD, but you have to wait for 5 min for those tests running.So if you want to skip git hooks while commting, simply run command like this:```bash git commit --no-verify -m "YOUR COMMIT MESSAGE" ```or for short```bash git commit -n -m "YOUR COMMIT MESSAGE" ```BTW, the command to push an empty commit is```bash git commit --allow-empty -m "YOUR COMMIT MESSAGE" ```Just remember only use these commands when you are really urgent and confident about your change.

Read More

How to fix: Enzyme expects an adapter to be configured

Working on updating a legacy React project. It's still using React 8 and Node 7.9, I'd like to upgrate it to the latest React and Node.The upgrading process was boring and straight forward, I just upgrade the dependencies and fix those broken changes in some dependencies, luckily most of them do noe need any change.#### ProblemBut when I run jest test, there is an error popup in almost every test file:``` Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none. To configure an adapter, you should call `Enzyme.configure({ adapter: new Adapter() })` before using any of Enzyme's top level APIs, where `Adapter` is the adapter corresponding to the library currently being tested. For example: import Adapter from 'enzyme-adapter-react-16'; ```Searched a bit, it looks like after Enzyme 3.0 and React15, you have to configure the `Adapter` to run the tests.#### SolutionTo fix it is very simple, just put these lines on the top of your test file:``` import { configure } from 'enzyme'; import Adapter from 'enzyme-adapter-react-15';configure({ adapter: new Adapter() }); ```While `enzyme-adapter-react-15` is also needed, you will need to use correct version per your react version.But, what if your project is very large, it has like more than 30 test files, is it mean that you will need to put these line on top of every of your test file?Of course no, here is what you should do.- create a js file, name it as `jestConfig.js` or any name you think is best. put it in any path in your project. - either create a `jest.config.js` in your project root or a block in `package.json` file - put/append these content into it:``` "jest": { // ... other jest configrations "setupFilesAfterEnv": ["/path/to/your/jestConfig.js"], } ```Notice that some tutorial might suggest you to do this with old Enzyme version``` "jest": { // ... other jest configrations "setupTestFrameworkScriptFile": "/path/to/your/jestConfig.js" } ```In newer version the key has been changed from `setupTestFrameworkScriptFile` to `setupFilesAfterEnv`, and you can use multiple config file since it now uses array as value.#### what about React 17While the most recent official `ezyme-adapter-react` is for 16, the 17 is not surpported yet, so if you are using React 17, here is another unofficial package you can use [@wojtekmaj/enzyme-adapter-react-17](https://www.npmjs.com/package/@wojtekmaj/enzyme-adapter-react-17)It has more than 6k downloads every week now, seems reliable.

Read More

为什么不要买罗技K380无线键盘

罗技K380无线键盘,被很多平台吹为几乎最佳的移动设备伴侣键盘,深受很多iPad乃至MacBook用户的欢迎。其优点有:蓝牙连接,最多可以连接三台设备,切换便捷。轻薄小巧,手感和笔记本键盘非常相似。而多色键帽更满足了不同审美用户的需求。因为有移动办公的需求,而Macbook的键盘手感实在太烂,所以前段时间我也买了一个,用了不多几次发现这是一个非常烂的键盘。手感差其实没有什么可以挑剔的,要做到轻薄小巧,必然牺牲键程和手感。但是其做工和用料却非常差,放在包里背了几次,就掉了一个键。不像其他键盘,键帽掉了以后按上去就可以了。K380的这个键帽我怎么装都装不上去。看了一下还在保修期内,就送回京东售后维修。然而京东最终给的答复是,键帽掉落属于外观损伤,不属于质量问题,拒绝保修。直接返回来了。返回后我一看,不但掉落的键没有装上去,旁边还多掉了一个键。仔细看了一下,之前掉落的键里面的卡件已经被按平,所以可以猜想是京东的售后人员先是尝试将按键按照传统方式按回去,暴力按了几下以后发现装不上, 然后又想拆旁边的键看一下结构怎么装。结果拆了以后没看出所以然来,新拆的这个键也装不回去了。就把键盘按键这种核心部件说成外观拒绝保修了。话说最近在另外一个网站看到有人的耳机罩在保修期内自动裂开,内部部件外露,送到京东也是以属于外观问题不予保修拒绝售后。不知道京东以相似的理由拒绝了多少保修。吐槽完京东,再仔细研究一下这个键盘,其剪刀脚结构是纯塑料,而且是非常脆弱的那种,稍微用点力就很容易变形乃至断掉,这个键就没用了。而键帽下面的连接件也是非常小而软的金属挂钩,很容易变形,当然也可以给掰回来,但是如果掰次数多了,就会断掉。也就是说,**如果你的K380键帽掉了,千万不要像其他键盘那样,尝试用力将键帽按回去,不但装不回去,还有可能永久损伤键帽甚至键帽下面的连接件**,连接件一旦坏了,键盘就废了,除非你能接受键盘少一个键。如果损伤键帽,淘宝上有几家卖的,估计就是送修或者报废的k380拆了卖零件的。一个键帽是10块钱起,下面的剪刀脚也是10块,不包邮。卖一排键帽就能买一个新的K380。所以如果你的K380键帽坏了,不要去买键帽,上淘宝或者其他二手平台卖键帽,然后拿换来的钱买一把新的键盘。当然买新键盘千万别再买k380了。

Read More