Code Monkey home page Code Monkey logo

blog's People

Contributors

legend80s avatar

Watchers

 avatar  avatar  avatar

blog's Issues

种族歧视性词语 Master Slave 和 Whitelist Blacklist 可替代词

谷歌决定放弃 Chrome 浏览器中“blacklist(黑名单)“、“whitelist(白名单)”的用法,后续使用“blocklist”和“allowlist”来替代它们。

本文仅代表个人观点。

master slave 这些词语对于遭受过贩卖和奴役的种族或人群来说每一次阅读都是对其心灵的深层次打击,会让其联想到祖辈受过的奴役之苦。而黑白名单 whitelist blacklist 会让人下意识将“白好黑坏”联系到一起,相比 master slave 而言,其直观的心理暗示更让其具备种族优越性的负面教育和挑唆性。

Google、GitHub 等都在替换种族歧视单词的行动中,虽然国内程序员在使用 whitelist / blacklist 和 master / slave 等单词的时候不会潜意识将其和种族歧视联想到一起。

github 准备将 master 重命名成 main
github 准备将 master 重命名为 main

微软、Github 已表示正在火速下架所有相关词汇编程词汇,把 master 换成main(主要的),slave 换成secondary(次要的)。

下架公司或组织:

  • Google
  • 微软
  • GitHub
  • Android Open Source Project (AOSP)
  • Go 语言
  • PHPUnit library
  • Curl
  • MySQL 数据库删除 master、黑名单白名单等术语

可代替中性单词

master slave 可代替单词,来自网络和 ietf

  • primary secondary
  • main secondary
  • leader follower
  • active standby
  • primary replica
  • writer reader
  • coordinator worker
  • parent helper

whitelist blacklist 可代替单词,来自网络和 ietf

  • allowlist denylist
  • allowlist blocklist
  • permit block

大家根据代码上下文选择其最合适的组合即可。

参考链接

最后

希望小小的声音能带来一些微小的变化,让这个世界变得美好一些

How Does Lodash Make its `parseInt` map-safe

先抛出结论:lodash 世界的 parseInt 是 map 安全的。

著名的 map 结合 parseInt bug

前端界著名的 parseInt bug:

['6', '08', '10'].map(parseInt);

// [6, NaN, 2]

为何用 lodash 的 parseInt 却是安全的,而且官方特意给出了示例

_.map(['6', '08', '10'], _.parseInt);

// => [6, 8, 10]

image

先从 trim 讲起

最近在做数组的 trim 操作,原本写法:

['001100', '110011'].map(val => _.trim(val))

但转念一想何不这么写,更简洁:

['001100', '110011'].map(_.trim)

完事后走路的途中突然想到,trim 有第二个参数,会当做 trim 的目标字符,即:

_.trim('110011', '1')

// => '000'

那么,结合 map 不是有 bug 吗,下标将会被当做 trim 的目标被删除掉,这是万万不可的,即:

['001100', '110011'].map(_.trim)

// => ['11', '00']

但实际上返回正确,仍然是 ["001100", "110011"]

为什么?

查看 lodash 源码发现有第三个秘密参数 guard,这是一个安全参数,当 truthy 则会省略第二个参数,故 _.trim 是 map 安全的。

官方文档并未有第三个参数的说明,因为:

The guard param is not intended for public use an is part of allowing it to work in _.map and others as an iteratee.

lodash/lodash#992 (comment)

大胆猜测 parseInt 也有第三个安全参数,果不其然。

image

总结

lodash 的 trim 和 parseInt 能够被安全的当做 map 的处理函数,甚至一系列将会被 map 消费的函数都被 lodash 设计得很安全。

lodash 很强大,解决了原生 js 诸多痼疾 👍。

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.