all files / scour/ scour.js

100% Statements 140/140
100% Branches 81/81
100% Functions 31/31
100% Lines 116/116
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891                                                                                          429× 314×   314× 314× 314×     314×                                                                                                                         60× 60× 60×                                                   24×     17× 17×                                                                                                                                       14× 12×             11×                                                                                                                                                     16× 14×   14× 10× 10×   67×       14×         14× 14×                                                       103× 99× 99×                             20× 14×                                                       32× 22×                                                                                                       15×   15×         11× 11×                                                                                                                                                                                                                                                       11× 11×                                                                                                                                                         20× 20× 47×   20×                                                                                                           132×                                         199× 199×                               33×   33×   59×                                                                                                                                     11×        
/* eslint-disable new-cap */
'use strict'
 
const sift = require('sift')
const assign = require('object-assign')
const buildExtensions = require('./lib/build_extensions')
const normalizeKeypath = require('./utilities/normalize_keypath')
const utils = require('./utilities')
const negate = require('./lib/negate')
const sortValues = require('./lib/sort_values')
const toFunction = require('to-function')
 
/**
 * scour : scour(object)
 * Returns a scour instance wrapping `object`.
 *
 *    scour(obj)
 *
 * It can be called on any Object or Array. (In fact, it can be called on
 * anything, but is only generally useful for Objects and Arrays.)
 *
 *     data = { menu: { visible: true, position: 'left' } }
 *     scour(data).get('menu.visible')
 *
 *     list = [ { id: 2 }, { id: 5 }, { id: 12 } ]
 *     scour(list).get('0.id')
 *
 * __Chaining__:
 * You can use it to start method chains. In fact, the intended use is to keep
 * your root [scour] object around, and chain from this.
 *
 *     db = scour({ menu: { visible: true, position: 'left' } })
 *
 *     // Elsewhere:
 *     menu = db.go('menu')
 *     menu.get('visible')
 *
 * __Properties__:
 * It the [root], [value] and [keypath] properties.
 *
 *    s = scour(obj)
 *    s.root             // => [scour object]
 *    s.value            // => raw data (that is, `obj`)
 *    s.keypath          // => string array
 *
 * __Accessing the value:__
 * You can access the raw data using [value].
 *
 *    db = scour(data)
 *    db.value               // => same as `data`
 *    db.go('users').value   // => same as `data.users`
 */
 
function scour (value, options) {
  if (!(this instanceof scour)) return new scour(value, options)
  this.value = value
 
  this.root = options && options.root || this
  this.keypath = options && options.keypath || []
  this.extensions = options && options.extensions || []
 
  // Apply any property extensions
  if (this.extensions.length) this.applyExtensions()
}
 
scour.prototype = {
  /**
   * Chaining methods:
   * (Section) These methods are used to traverse nested structures. All these
   * methods return [scour] instances, making them suitable for chaining.
   *
   * #### On null values
   * Note that `undefined`, `false` and `null` values are still [scour]-wrapped
   * when returned from [go()], [at()] and [find()].
   *
   *     list = [ { name: 'Homer' }, { name: 'Bart' } ]
   *
   *     scour(list).at(4)         // => [ scour undefined ]
   *     scour(list).at(4).value   // => undefined
   *
   * This is done so that you can chain methods safely even when something is null.
   * This behavior is consistent with what you'd expect with jQuery.
   *
   *     data = { users: { ... } }
   *     db = scour(data)
   *
   *     db.go('blogposts').map((post) => post.get('title'))
   *     // => []
   */
 
  /**
   * go : go(keypath...)
   * Navigates down to a given `keypath`. Always returns a [scour] instance.
   * Rules [on null values] apply.
   *
   *     data =
   *       { users:
   *         { 12: { name: 'steve', last: 'jobs' },
   *           23: { name: 'bill', last: 'gates' } } }
   *
   *     scour(data).go('users')                    // => [scour (users)]
   *     scour(data).go('users', '12')              // => [scour (name, last)]
   *     scour(data).go('users', '12').get('name')  // => 'steve'
   *
   * __Dot notation:__
   * Keypaths can be given in dot notation or as an array. These statements are
   * equivalent.
   *
   *     scour(data).go('users.12')
   *     scour(data).go('users', '12')
   *     scour(data).go(['users', '12'])
   *
   * __Non-objects:__
   * If you use it on a non-object or non-array value, it will still be
   * returned as a [scour] instance. This is not likely what you want; use
   * [get()] instead.
   *
   *     attr = scour(data).go('users', '12', 'name')
   *     attr           // => [scour object]
   *     attr.value     // => 'steve'
   *     attr.keypath   // => ['users', '12', 'name']
   */
 
  go () {
    const keypath = normalizeKeypath(arguments, true)
    const result = this.get.apply(this, keypath)
    return this._get(result, keypath)
  },
 
  /**
   * Internal: gathers multiple keys; not used yet
   */
 
  gather (keypaths) {
    var result
    if (Array.isArray(this.value)) {
      result = keypaths.map((key, val) => this.get(key))
    } else {
      result = utils.indexedMap(keypaths, (key) => [ key, this.get(key) ])
    }
    return this.reset(result)
  },
 
  /**
   * Returns the item at `index`. This differs from `go` as this searches by
   * index, not by key. This returns a the raw value, unlike [getAt()]. Rules
   * [on null values] apply.
   *
   *     users =
   *       { 12: { name: 'steve' },
   *         23: { name: 'bill' } }
   *
   *     scour(users).at(0)          // => [scour { name: 'steve' }]
   *     scour(users).get(12)        // => [scour { name: 'steve' }]
   */
 
  at (index) {
    if (Array.isArray(this.value)) {
      return this._get(this.value[index], [ '' + index ])
    }
 
    const key = this.keys()[index]
    return this._get(key && this.value[key], [ '' + key ])
  },
 
  /**
   * Returns the item at `index`. This differs from `get` as this searches by
   * index, not by key. This returns a the raw value, unlike [at()].
   *
   *     users =
   *       { 12: { name: 'steve' },
   *         23: { name: 'bill' } }
   *
   *     scour(users).at(0)           // => [scour { name: 'steve' }]
   *     scour(users).getAt(0)        // => { name: 'steve' }
   */
 
  getAt (index) {
    if (Array.isArray(this.value)) return this.value[index]
    const key = this.keys()[index]
    return key && this.value[key]
  },
 
  /**
   * Sifts through the values and returns a set that matches given
   * `conditions`. Supports simple objects, MongoDB-style
   * queries, and functions.
   *
   *     scour(data).filter({ name: 'Moe' })
   *     scour(data).filter({ name: { $in: ['Larry', 'Curly'] })
   *     scour(data).filter((item) => item.get('name') === 'Moe')
   *
   * __Filter by object:__
   * If you pass an object as a condition, `filter()` will check if that object
   * coincides with the objects in the collection.
   *
   *     scour(data).filter({ name: 'Moe' })
   *
   * __Filter by function:__
   * You may pass a function as a parameter. In this case, the `item` being
   * passed to the callback will be a [scour]-wrapped object. The result
   * will also be a [scour]-wrapped object, making it chainable.
   *
   *     scour(data)
   *       .filter((item, key) => +item.get('price') > 200)
   *       .sortBy('price')
   *       .first()
   *
   * __Advanced queries:__
   * MongoDB-style queries are supported as provided by [sift.js].  For
   * reference, see [MongoDB Query Operators][query-ops].
   *
   *     scour(products).filter({ price: { $gt: 200 })
   *     scour(articles).filter({ published_at: { $not: null }})
   *
   * __Arrays or objects:__
   * Both arrays and array-like objects are supported. In this example below,
   * an object will be used as the input.
   *
   *     devices =
   *       { 1: { id: 1, name: 'Phone', mobile: true },
   *         2: { id: 2, name: 'Tablet', mobile: true },
   *         3: { id: 3, name: 'Desktop', mobile: false } }
   *
   *     scour(devices).filter({ mobile: true }).len()
   *     // => 2
   *
   * Also see [scour.filter()] for the unwrapped version.
   *
   * [query-ops]: https://docs.mongodb.org/manual/reference/operator/query/
   */
 
  filter (conditions) {
    if (!this.value) return this.reset([])
    if (typeof conditions === 'function') {
      return this.filterByFunction(conditions)
    }
    return this.reset(sift(conditions, this.value))
  },
 
  filterByFunction (fn) {
    var isArray = Array.isArray(this.value)
    var result
 
    if (isArray) {
      result = []
      this.each((val, key) => fn(val, key) && result.push(val.value))
    } else {
      result = {}
      this.each((val, key) => { if (fn(val, key)) result[key] = val.value })
    }
 
    return this.reset(result)
  },
 
  /**
   * Inverse of [filter()] -- see `filter()` documentation for details.
   */
 
  reject (conditions) {
    if (!this.value) return this.reset([])
    if (typeof conditions === 'function') {
      return this.filterByFunction(negate(conditions))
    } else {
      return this.filter({ $not: conditions })
    }
  },
 
  /**
   * Returns the first value that matches `conditions`.  Supports MongoDB-style
   * queries. For reference, see [MongoDB Query Operators][query-ops]. Also
   * see [filter()], as this is functionally-equivalent to the first result of
   * `filter()`. Rules [on null values] apply.
   *
   * [query-ops]: https://docs.mongodb.org/manual/reference/operator/query/
   *
   *     scour(data).find({ name: 'john' })
   *     scour(data).find({ name: { $in: ['moe', 'larry'] })
   */
 
  find (conditions) {
    const key = sift.keyOf(conditions, this.value)
    if (typeof key === 'undefined') return
    return this._get(this.value[key], [key])
  },
 
  /**
   * Returns the first result as a [scour]-wrapped object. This is equivalent
   * to [at(0)](#at).
   */
 
  first () {
    return this.at(0)
  },
 
  /**
   * Returns the first result as a [scour]-wrapped object. This is equivalent
   * to `at(len() - 1)`: see [at()] and [len()].
   */
 
  last () {
    var len = this.len()
    return this.at(len - 1)
  },
 
  /**
   * Sorts a collection. Returns a [scour]-wrapped object suitable for
   * chaining. Like other chainable methods, this works on arrays as well as
   * objects.
   *
   *     data =
   *       { 0: { name: 'Wilma' },
   *         1: { name: 'Barney' },
   *         2: { name: 'Fred' } }
   *
   *     scour(data).sortBy('name').value
   *     // { 1: { name: 'Barney' },
   *     //   2: { name: 'Fred' },
   *     //   0: { name: 'Wilma' } }
   *
   * __Conditions:__
   * The given condition can be a string or a function. When it's given as a
   * function, the `item` being passed is a [scour]-wrapped object, just like
   * in [forEach()] (et al). These two examples below are
   * functionally-equivalent.
   *
   *     scour(data).sortBy('name')
   *     scour(data).sortBy((item) => item.get('name'))
   *
   * You may also define nested keys in dot-notation:
   *
   *     scour(data).sortBy('user.name')
   */
 
  sortBy (condition) {
    if (!this.value) return this.reset([])
    var values
 
    if (typeof condition === 'string') {
      var key = condition
      condition = toFunction(key)
      // don't use `this.map` or `this.each` so we skip `new scour()`
      values = utils.map(this.value, (value, key, index) => ({
        key, value, criteria: condition(value, key), index
      }))
    } else {
      values = this.map((value, key, index) => ({
        key, value: value.value, criteria: condition(value, key), index
      }))
    }
 
    var sorted = sortValues(values, Array.isArray(this.value))
    return this.reset(sorted)
  },
 
  /**
   * Reading methods:
   * (Section) For retrieving data.
   */
 
  /**
   * get : get(keypath...)
   * Returns data in a given `keypath`.
   *
   *     data =
   *       { users:
   *         { 12: { name: 'steve' },
   *           23: { name: 'bill' } } }
   *
   *     scour(data).get('users')       // => same as data.users
   *     scour(data).go('users').value  // => same as data.users
   *
   * __Dot notation:__
   * Like [go()], the `keypath` can be given in dot notation.
   *
   *     scour(data).get('books.featured.name')
   *     scour(data).get('books', 'featured', 'name')
   */
 
  get () {
    if (!this.value) return
    const keypath = normalizeKeypath(arguments, true)
    return utils.get(this.value, keypath)
  },
 
  /**
   * Returns the length of the object or array. For objects, it returns the
   * number of keys.
   *
   *     users =
   *       { 12: { name: 'steve' },
   *         23: { name: 'bill' } }
   *
   *     names = scour(users).len()  // => 2
   */
 
  len () {
    if (!this.value) return 0
    if (Array.isArray(this.value)) return this.value.length
    return this.keys().length
  },
 
  /**
   * Returns an array. If the the value is an object, it returns the values of
   * that object. If the value is an array, it returns it as is. Also aliased
   * as `values()`.
   *
   *     users =
   *       { 12: { name: 'steve' },
   *         23: { name: 'bill' } }
   *
   *     names = scour(users).toArray()
   *     // => [ {name: 'steve'}, {name: 'bill'} ]
   */
 
  toArray () {
    if (Array.isArray(this.value)) return this.value
    return scour.map(this.value, (val, key) => val)
  },
 
  values () {
    return this.toArray()
  },
 
  /**
   * Returns keys. If the value is an array, this returns the array's indices.
   * Also see [toArray()] to retrieve the values instead.
   */
 
  keys () {
    if (!this.value) return []
    return Object.keys(this.value)
  },
 
  /**
   * Writing methods:
   * (Section) These are methods for modifying an object/array tree immutably.
   * Note that all these functions are immutable--it will not modify existing
   * data, but rather spawn new objects with the modifications done on them.
   */
 
  /**
   * Sets values immutably. Returns a copy of the same object ([scour]-wrapped)
   * with the modifications applied.
   *
   *     data = { bob: { name: 'Bob' } }
   *     db = scour(data)
   *     db.set([ 'bob', 'name' ], 'Robert')
   *     // db.value == { bob: { name: 'Robert' } }
   *
   * __Immutability:__
   * This is an immutable function, and will return a new object. It won't
   * modify your original object.
   *
   *     profile = scour({ name: 'John' })
   *     profile2 = profile.set('email', 'john@gmail.com')
   *
   *     profile.value   // => { name: 'John' }
   *     profile2.value  // => { name: 'John', email: 'john@gmail.com' }
   *
   * __Using within a scope:__
   * Be aware that using all writing methods ([set()], [del()], [extend()]) on
   * scoped objects (ie, made with [go()]) will spawn a new [root] object. If
   * you're keeping a reference to the root object, you'll need to update it
   * accordingly.
   *
   *     db = scour(data)
   *     book = db.go('book')
   *     book.root === db       // correct so far
   *
   *     book = book.set('title', 'IQ84')
   *     book = book.del('sale_price')
   *     book.root !== db      // `root` has been updated
   *
   * __Dot notation:__
   * Like [go()] and [get()], the keypath can be given in dot notation or an
   * array.
   *
   *     scour(data).set('menu.left.visible', true)
   *     scour(data).set(['menu', 'left', 'visible'], true)
   */
 
  set (keypath, value) {
    keypath = normalizeKeypath(keypath)
 
    if (this.root !== this) {
      return this.root
        .set(this.keypath.concat(keypath), value).go(this.keypath)
    }
 
    // use .valueOf() to denature any scour-wrapping or String() or whatnot
    const result = scour.set(this.value || {}, keypath, value.valueOf())
    return this.reset(result, { root: null })
  },
 
  /**
   * Deletes values immutably. Returns a copy of the same object
   * ([scour]-wrapped) with the modifications applied.
   *
   * Like [set()], the keypath can be given in dot notation or an
   * array.
   *
   *    scour(data).del('menu.left.visible')
   *    scour(data).del(['menu', 'left', 'visible'])
   *
   * See [set()] for more information on working with immutables.
   */
 
  del (keypath) {
    if (!this.value) return this
    keypath = normalizeKeypath(keypath)
 
    if (this.root !== this) {
      return this.root.del(this.keypath.concat(keypath)).go(this.keypath)
    }
 
    const result = scour.del(this.value, keypath)
    return this.reset(result, { root: null })
  },
 
  /**
   * extend : extend(objects...)
   * Extends the data with more values. Returns a [scour]-wrapped object. Only
   * supports objects; arrays and non-objects will return undefined. Just like
   * [Object.assign], you may pass multiple objects to the parameters.
   *
   *    data  = { a: 1, b: 2 }
   *    data2 = scour(data).extend({ c: 3 })
   *
   *    data2  // => [scour { a: 1, b: 2, c: 3 }]
   *    data2.value   // => { a: 1, b: 2, c: 3 }
   *
   * See [set()] for more information on working with immutables.
   */
 
  extend () {
    if (typeof this.value !== 'object' || Array.isArray(this.value)) return
    var result = {}
    assign(result, this.value)
    for (var i = 0, len = arguments.length; i < len; i++) {
      if (typeof arguments[i] !== 'object') return
      assign(result, arguments[i])
    }
 
    if (this.root !== this) {
      return this.root.set(this.keypath, result).go(this.keypath)
    }
 
    return this.reset(result, { root: false })
  },
 
  /**
   * Utility methods:
   * (Section) For stuff.
   */
 
  /**
   * use : use(extensions)
   * Extends functionality for certain keypaths with custom methods.
   * See [Extensions example] for examples.
   *
   *     data =
   *       { users:
   *         { 12: { name: 'steve', surname: 'jobs' },
   *           23: { name: 'bill', surname: 'gates' } } }
   *
   *     extensions = {
   *       'users.*': {
   *         fullname () {
   *           return this.get('name') + ' ' + this.get('surname')
   *         }
   *       }
   *     }
   *
   *     scour(data)
   *       .use(extensions)
   *       .get('users', 12)
   *       .fullname()       // => 'bill gates'
   *
   * __Extensions format:__
   * The parameter `extension` is an object, with keys being keypath globs, and
   * values being properties to be extended.
   *
   *     .use({
   *       'books.*': { ... },
   *       'authors.*': { ... },
   *       'publishers.*': { ... }
   *      })
   *
   * __Extending root:__
   * To bind properties to the root method, use an empty string as the keypath.
   *
   *     .use({
   *       '': {
   *         users() { return this.go('users') },
   *         authors() { return this.go('authors') }
   *       }
   *     })
   *
   * __Keypath filtering:__
   * You can use glob-like `*` and `**` to match parts of a keypath. A `*` will
   * match any one segment, and `**` will match one or many segments. Here are
   * some examples:
   *
   * - `users.*` - will match `users.1`, but not `users.1.photos`
   * - `users.**` - will match `users.1.photos`
   * - `users.*.photos` - will match `users.1.photos`
   * - `**` will match anything
   *
   * __When using outside root:__
   * Any extensions in a scoped object (ie, made with [go()]) will be used relative
   * to it. For instance, if you define an extension to `admins.*` inside
   * `.go('users')`, it will affect `users.
   *
   *     data = { users: { john: { } }
   *     db = scour(data)
   *
   *     users = db.go('users')
   *       .use({ '*': { hasName () { return !!this.get('name') } })
   *
   *     users.go('john').hasName()      // works
   *
   * While this is supported, it is *not* recommended: these extensions will not
   * propagate back to the root, and any objects taken from the root will not
   * have those extensions applied to them.
   *
   *     users.go('john').hasName()              // works
   *     db.go('users.john').hasName()           // doesn't work
   */
 
  use (spec) {
    const extensions = buildExtensions(this.keypath, spec)
    if (this.root === this) {
      return this.reset(this.value, { extensions, root: null })
    } else {
      // Spawn a new `root` with the extensions applied
      return this.root
        .reset(this.root.value, { extensions, root: null })
        .reset(this.value, { keypath: this.keypath })
    }
  },
 
  /**
   * Returns the value for serialization. This allows `JSON.stringify()` to
   * work with `scour`-wrapped objects. The name of this method is a bit
   * confusing, as it doesn't actually return a JSON string — but I'm afraid
   * that it's the way that the JavaScript API for [JSON.stringify] works.
   *
   * [JSON.stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON%28%29_behavior
   */
 
  toJSON () {
    return this.value
  },
 
  valueOf () {
    return this.value
  },
 
  toString () {
    return `[scour (${this.keys().join(', ')})]`
  },
 
  /**
   * Iteration methods:
   * (Section) These methods are generally useful for collections. These
   * methods can work with either arrays or array-like objects, such as
   * below.
   *
   *     subjects =
   *       { 1: { id: 1, title: 'Math', level: 101 },
   *         2: { id: 2, title: 'Science', level: 103 },
   *         3: { id: 3, title: 'History', level: 102 } }
   *
   * __Values:__
   * For all these functions, The items passed onto the callbacks _is_ a
   * [scour]-wrapped object. Use `item.value` or `this` to access the raw
   * values.
   *
   *     scour(subjects).forEach((subject, key) => {
   *       console.log(subject.get('title'))
   *     })
   *
   * __Return values:__
   * For methods that return values (such as [map()], the returned results _is
   * not_ a [scour]-wrapped object, and isn't suitable for chaining.
   *
   *     scour(subjects).map((subject, key) => {
   *       return subject.get('title') + ' ' + subject.get('level')
   *     })
   *     // => [ 'Math 101', 'Science 103', 'History 102' ]
   */
 
  /**
   * forEach : forEach(function(item, key, index))
   * Loops through each item. Supports both arrays and objects.
   * The rules specified in [Iteration methods] apply.
   *
   *     users =
   *       { 12: { name: 'steve' },
   *         23: { name: 'bill' } }
   *
   *     scour(users).each((user, key) => {
   *       console.log(user.get('name'))
   *     })
   *
   * The values passed onto the function are:
   *
   * - `item` - the value; always a scour object.
   * - `key` - the key.
   * - `index` - the index.
   */
 
  forEach (fn) {
    let index = 0
    scour.each(this.value, (val, key) => {
      fn.call(val, this._get(val, [key]), key, index++)
    })
    return this
  },
 
  /**
   * Alias for [forEach](#foreach).
   */
 
  each (fn) {
    return this.forEach(fn)
  },
 
  /**
   * map : map(function(item, key))
   * Loops through each item and returns an array based on the iterator's
   * return values. Supports both arrays and objects.
   * The rules specified in [Iteration methods] apply.
   *
   *     users =
   *       { 12: { name: 'Steve' },
   *         23: { name: 'Bill' } }
   *
   *     names = scour(users).map((user, key) => user.get('name'))
   *     // => [ 'Steve', 'Bill' ]
   */
 
  map: thisify(utils.map),
 
  /**
   * mapObject : mapObject(function(val, key))
   * Creates a new `Object` with with the results of calling a provided function
   * on every element in this array. Works like [Array#map], but also works on
   * objects as well as arrays, and it returns an object instead.
   * The rules specified in [Iteration methods] apply.
   *
   * See [scour.mapObject()] for details and the non-wrapped version.
   */
 
  mapObject: thisify(utils.mapObject),
 
  /**
   * indexedMap : indexedMap(function(val, key))
   * Creates a new `Object` with with the results of calling a provided function
   * returning the keys and values for the new object.
   * The rules specified in [Iteration methods] apply.
   *
   * See [scour.indexedMap()] for details and the non-wrapped version.
   */
 
  indexedMap: thisify(utils.indexedMap),
 
  /**
   * Internal: spawns an instance with a given data and keypath.
   */
 
  _get (result, keypath) {
    return this.reset(result, {
      keypath: this.keypath.concat(keypath)
    })
  },
 
  /**
   * Returns a clone with the `value` replaced. The new instance will
   * retain the same properties, so things like [use()] extensions are carried
   * over.
   *
   *     db = scour({ name: 'hello' })
   *     db.value  //=> { name: 'hello' }
   *
   *     db = db.reset({})
   *     db.value  // => {}
   *
   * This is useful for, say, using Scour with [Redux] and implementing an
   * action to reset the state back to empty.
   */
 
  reset (value, options) {
    const op = options || {}
    return new scour(value, {
      root:
        typeof op.root !== 'undefined' ? op.root : this.root,
      keypath:
        typeof op.keypath !== 'undefined' ? op.keypath : this.keypath,
      extensions: typeof op.extensions !== 'undefined'
        ? this.extensions.concat(op.extensions)
        : this.extensions
    })
  },
 
  /**
   * Internal: applies extensions
   */
 
  applyExtensions () {
    var path = this.keypath.join('.')
 
    this.extensions.forEach((extension) => {
      // extension is [ RegExp, properties object ]
      if (extension[0].test(path)) assign(this, extension[1])
    })
  }
}
 
/**
 * Attributes:
 * (Section) These attributes are available to [scour] instances.
 */
 
/**
 * value : value
 * The raw value being wrapped. You can use this to terminate a chained call.
 *
 *     users =
 *       [ { name: 'john', admin: true },
 *         { name: 'kyle', admin: false } ]
 *
 *     scour(users)
 *       .filter({ admin: true })
 *       .value
 *     // => [ { name: 'john', admin: true } ]
 */
 
/**
 * root : root
 * A reference to the root [scour] instance.
 * Everytime you traverse using [go()], a new [scour] object is spawned that's
 * scoped to a keypath.  Each of these [scour] objects have a `root` attribute
 * that's a reference to the top-level [scour] object.
 *
 *     db = scour(...)
 *
 *     photos = db.go('photos')
 *     photos.root    // => same as `db`
 *
 * This allows you to return to the root when needed.
 *
 *     db = scour(...)
 *     artist = db.go('artists', '9328')
 *     artist.root.go('albums').find({ artist_id: artist.get('id') })
 */
 
/**
 * keypath : keypath
 * An array of strings representing each step in how deep the current scope is
 * relative to the root. Each time you traverse using [go()], a new [scour]
 * object is spawned.
 *
 *     db = scour(...)
 *
 *     users = db.go('users')
 *     users.keypath            // => ['users']
 *
 *     admins = users.go('admins')
 *     admins.keypath           // => ['users', 'admins']
 *
 *     user = admins.go('23')
 *     user.keypath             // => ['users', 'admins', '23']
 */
 
// Export utilities
assign(scour, utils)
 
/**
 * Internal: decorates collection functions
 */
 
function thisify (fn) {
  return function () {
    return fn.bind(null, this.forEach.bind(this)).apply(this, arguments)
  }
}
 
module.exports = scour