├── .gitignore ├── bin └── build-examples.js ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /examples.html 2 | -------------------------------------------------------------------------------- /bin/build-examples.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var fs = require('fs'); 3 | var path = require('path'); 4 | var marked = require('marked'); 5 | 6 | marked.setOptions({ 7 | langPrefix: 'language-' 8 | }); 9 | 10 | var renderer = new marked.Renderer(); 11 | 12 | renderer.list = function(body, ordered) { 13 | var type = ordered ? 'ol' : 'ul'; 14 | return '<' + type + ' class="ESComparison">\n' + body + '\n'; 15 | }; 16 | 17 | renderer.listitem = function(text) { 18 | return '
  • ' + text + '
  • \n'; 19 | }; 20 | 21 | var markdownFilename = path.join(__dirname, '..', 'README.md'); 22 | var htmlFilename = path.join(__dirname, '..', 'examples.html'); 23 | 24 | var markdown = fs.readFileSync(markdownFilename, { encoding: 'utf8' }); 25 | fs.writeFileSync( 26 | htmlFilename, 27 | marked(markdown, { renderer: renderer }), 28 | { encoding: 'utf8' } 29 | ); 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-present, Reindex Software 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Arrays 2 | 3 | #### Iterate 4 | 5 | * Underscore 6 | 7 | ```javascript 8 | _.each(array, iteratee) 9 | ``` 10 | 11 | * ES5.1 12 | 13 | ```javascript 14 | array.forEach(iteratee) 15 | ``` 16 | 17 | #### Map 18 | 19 | * Underscore 20 | 21 | ```javascript 22 | _.map(array, iteratee) 23 | ``` 24 | 25 | * ES5.1 26 | 27 | ```javascript 28 | array.map(iteratee) 29 | ``` 30 | 31 | #### Use a function to accumulate a single value from an array (left-to-right) 32 | 33 | * Underscore 34 | 35 | ```javascript 36 | _.reduce(array, iteratee, memo) 37 | ``` 38 | 39 | * ES5.1 40 | 41 | ```javascript 42 | array.reduce(iteratee, memo) 43 | ``` 44 | 45 | 46 | #### Use a function to accumulate a single value from an array (right-to-left) 47 | 48 | * Underscore 49 | 50 | ```javascript 51 | _.reduceRight(array, iteratee, memo) 52 | ``` 53 | 54 | * ES5.1 55 | 56 | ```javascript 57 | array.reduceRight(iteratee, memo) 58 | ``` 59 | 60 | #### Test whether all elements in an array pass a predicate 61 | 62 | * Underscore 63 | 64 | ```javascript 65 | _.every(array, predicate) 66 | ``` 67 | 68 | * ES5.1 69 | 70 | ```javascript 71 | array.every(predicate) 72 | ``` 73 | 74 | #### Test whether some element in an array passes a predicate 75 | 76 | * Underscore 77 | 78 | ```javascript 79 | _.some(array, predicate) 80 | ``` 81 | 82 | * ES5.1 83 | 84 | ```javascript 85 | array.some(predicate) 86 | ``` 87 | 88 | #### Find a value in an array 89 | 90 | * Underscore 91 | 92 | ```javascript 93 | _.find(array, predicate) 94 | ``` 95 | 96 | * ES2015 97 | 98 | ```javascript 99 | array.find(predicate) 100 | ``` 101 | 102 | #### Get a property from each element in an array 103 | 104 | * Underscore 105 | 106 | ```javascript 107 | _.pluck(array, propertyName) 108 | ``` 109 | 110 | * ES2015 111 | 112 | ```javascript 113 | array.map(value => value[propertyName]) 114 | ``` 115 | 116 | #### Check if array includes an element 117 | 118 | * Underscore 119 | 120 | ```javascript 121 | _.includes(array, element) 122 | ``` 123 | 124 | * ES2016 125 | 126 | ```javascript 127 | array.includes(element) 128 | ``` 129 | 130 | #### Convert an array-like object to array 131 | 132 | * Underscore 133 | 134 | ```javascript 135 | _.toArray(arguments) 136 | ``` 137 | 138 | * ES2015 139 | 140 | ```javascript 141 | [...arguments] 142 | ``` 143 | 144 | #### Convert an array of keys and values to an object 145 | 146 | * Underscore 147 | 148 | ```javascript 149 | _.object(array) 150 | ``` 151 | 152 | * ES2015 153 | 154 | ```javascript 155 | array.reduce((result, [key, val]) => Object.assign(result, {[key]: val}), {}) 156 | ``` 157 | 158 | * Object Rest/Spread (Stage 2) 159 | 160 | ```javascript 161 | array.reduce((result, [key, val]) => {...result, [key]: val}, {}) 162 | ``` 163 | 164 | #### Create a copy of an array with all falsy values removed 165 | 166 | * Underscore 167 | 168 | ```javascript 169 | _.compact(array) 170 | ``` 171 | 172 | * ES5.1 173 | 174 | ```javascript 175 | array.filter(Boolean) 176 | ``` 177 | 178 | * ES2015 179 | 180 | ```javascript 181 | array.filter(x => !!x) 182 | ``` 183 | 184 | #### Create a copy of an array with duplicates removed 185 | 186 | * Underscore 187 | 188 | ```javascript 189 | _.uniq(array) 190 | ``` 191 | 192 | * ES2015 193 | 194 | ```javascript 195 | [...new Set(array)] 196 | ``` 197 | 198 | #### Find the index of a value in an array 199 | 200 | * Underscore 201 | 202 | ```javascript 203 | _.indexOf(array, value) 204 | ``` 205 | 206 | * ES5.1 207 | 208 | ```javascript 209 | array.indexOf(value) 210 | ``` 211 | 212 | #### Find the index in an array by predicate 213 | 214 | * Underscore 215 | 216 | ```javascript 217 | _.findIndex([4, 6, 7, 12], isPrime); 218 | ``` 219 | 220 | * ES2015 221 | 222 | ```javascript 223 | [4, 6, 7, 12].findIndex(isPrime); 224 | ``` 225 | 226 | #### Create an array with n numbers, starting from x 227 | 228 | * Underscore 229 | 230 | ```javascript 231 | _.range(x, x + n) 232 | ``` 233 | 234 | * ES2015 235 | 236 | ```javascript 237 | Array.from(Array(n), (_, i) => x + i) 238 | ``` 239 | 240 | ### Objects 241 | 242 | #### Names of own enumerable properties as an array 243 | 244 | * Underscore 245 | 246 | ```javascript 247 | _.keys(object) 248 | ``` 249 | 250 | * ES5.1 251 | 252 | ```javascript 253 | Object.keys(object) 254 | ``` 255 | 256 | #### Number of keys in an object 257 | 258 | * Underscore 259 | 260 | ```javascript 261 | _.size(object) 262 | ``` 263 | 264 | * ES5.1 265 | 266 | ```javascript 267 | Object.keys(object).length 268 | ``` 269 | 270 | #### Names of all enumerable properties as an array 271 | 272 | * Underscore 273 | 274 | ```javascript 275 | _.allKeys(object) 276 | ``` 277 | 278 | * ES2015 279 | 280 | ```javascript 281 | [...Reflect.enumerate(object)] 282 | ``` 283 | 284 | #### Values 285 | 286 | * Underscore 287 | 288 | ```javascript 289 | _.values(object) 290 | ``` 291 | 292 | * ES2015 293 | 294 | ```javascript 295 | Object.keys(object).map(key => object[key]) 296 | ``` 297 | 298 | * ES2017 299 | 300 | ```javascript 301 | Object.values(object) 302 | ``` 303 | 304 | #### Create a new object with the given prototype and properties 305 | 306 | * Underscore 307 | 308 | ```javascript 309 | _.create(proto, properties) 310 | ``` 311 | 312 | * ES2015 313 | 314 | ```javascript 315 | Object.assign(Object.create(proto), properties) 316 | ``` 317 | 318 | #### Create a new object from merged own properties 319 | 320 | * Underscore 321 | 322 | ```javascript 323 | _.assign({}, source, { a: false }) 324 | ``` 325 | 326 | * ES2015 327 | 328 | ```javascript 329 | Object.assign({}, source, { a: false }) 330 | ``` 331 | 332 | * Object Rest/Spread (Stage 2) 333 | 334 | ```javascript 335 | { ...source, a: false } 336 | ``` 337 | 338 | #### Create a shallow clone of own properties of an object 339 | 340 | * Underscore 341 | 342 | ```javascript 343 | _.extendOwn({}, object) 344 | ``` 345 | 346 | * Object Rest/Spread (Stage 2) 347 | 348 | ```javascript 349 | { ...object } 350 | ``` 351 | 352 | #### Check if an object is an array 353 | 354 | * Underscore 355 | 356 | ```javascript 357 | _.isArray(object) 358 | ``` 359 | 360 | * ES5.1 361 | 362 | ```javascript 363 | Array.isArray(object) 364 | ``` 365 | 366 | #### Check if an object is a finite Number 367 | 368 | * Underscore 369 | 370 | ```javascript 371 | _.isNumber(object) && _.isFinite(object) 372 | ``` 373 | 374 | * ES2015 375 | 376 | ```javascript 377 | Number.isFinite(object) 378 | ``` 379 | 380 | ### Functions 381 | 382 | #### Bind a function to an object 383 | 384 | * Underscore 385 | 386 | ```javascript 387 | foo(_.bind(function () { 388 | this.bar(); 389 | }, this)); 390 | 391 | foo(_.bind(object.fun, object)); 392 | ``` 393 | 394 | * ES2015 395 | 396 | ```javascript 397 | foo(() => { 398 | this.bar(); 399 | }); 400 | 401 | foo(() => object.fun()); 402 | ``` 403 | 404 | ### Utility 405 | 406 | #### Identity function 407 | 408 | * Underscore 409 | 410 | ```javascript 411 | _.identity 412 | ``` 413 | 414 | * ES2015 415 | 416 | ```javascript 417 | value => value 418 | ``` 419 | 420 | #### A function that returns a value 421 | 422 | * Underscore 423 | 424 | ```javascript 425 | _.constant(value) 426 | ``` 427 | 428 | * ES2015 429 | 430 | ```javascript 431 | () => value 432 | ``` 433 | 434 | #### The empty function 435 | 436 | * Underscore 437 | 438 | ```javascript 439 | _.noop 440 | ``` 441 | 442 | * ES2015 443 | 444 | ```javascript 445 | () => {} 446 | ``` 447 | 448 | #### Get the current time in milliseconds since the epoch 449 | 450 | * Underscore 451 | 452 | ```javascript 453 | _.now() 454 | ``` 455 | 456 | * ES5.1 457 | 458 | ```javascript 459 | Date.now() 460 | ``` 461 | 462 | #### Template 463 | 464 | * Underscore 465 | 466 | ```javascript 467 | var greeting = _.template("hello <%= name %>"); 468 | greeting({ name: 'moe' }); 469 | ``` 470 | 471 | * ES2015 472 | 473 | ```javascript 474 | const greeting = ({ name }) => `hello ${name}`; 475 | greeting({ name: 'moe' }); 476 | ``` 477 | --------------------------------------------------------------------------------