Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:56:21+00:00 2026-06-03T13:56:21+00:00

Possible Duplicate: is object empty? update: (id, data) -> toUpdate = @find(id) if toUpdate

  • 0

Possible Duplicate:
is object empty?

 update: (id, data) ->
    toUpdate = @find(id)
    if toUpdate isnt {}
            console.log "hi mom"
            console.log toUpdate
            toUpdate.setProperty(key, value) for own key, value of data

    return toUpdate

 find:(id) ->
    result = record for record in @storage when record.id is id
    return result or {}

Given the following Mocha tests

describe '#update', ->
    it 'should return an updated record from a given id and data when the record exists', ->
      boogie = createData()
      archive = new Archive("Dog")
      dog = archive.create(boogie)
      result = archive.update(1, {name:"Chompie", age:1})
      result.name.should.eql "Chompie"
      result.age.should.eql 1
      result.emotion.should.eql dog.emotion

    it 'should return an updated record from a given id and data when the record does not exist', ->
      boogie = createData()
      archive = new Archive("Dog")
      dog = archive.create(boogie)
      result = archive.update(50, {name:"Chompie", age:1})
      result.should.not.exist

The result is

Archive #update should return an updated record from a given id and data when the record exists: hi mom
{ id: 1,
  validationStrategies: {},
  name: 'Boogie',
  age: 2,
  emotion: 'happy' }
  ✓ Archive #update should return an updated record from a given id and data when the record exists: 1ms
    Archive #update should return empty when the record does not exist: hi mom
{}
    ✖ 1 of 13 tests failed:
    1) Archive #update should return empty when the record does not exist:
    TypeError: Object #<Object> has no method 'setProperty'

…surprising, isnt it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:56:23+00:00Added an answer on June 3, 2026 at 1:56 pm

    CoffeeScript’s is (AKA ==) is just JavaScript’s === and isnt (AKA !=) is just JavaScript’s !==. So your condition:

    if toUpdate isnt {}
    

    will always be true since toUpdate and the object literal {} will never be the same object.

    However, if @find could return a known “empty” object that was available in a constant, then you could use isnt:

    EMPTY = {}
    
    find: ->
        # ...
        EMPTY
    

    and later:

    if toUpdate isnt EMPTY
        #...
    

    For example, consider this simple code:

    a = { }
    b = { }
    console.log("a is b: #{a is b}")
    console.log("a isnt b: #{a isnt b}")
    

    That will give you this in your console:

    a is b: false
    a isnt b: true
    

    But this:

    class C
        EMPTY = { }
        find: -> EMPTY
        check: -> console.log("@find() == EMPTY: #{@find() == EMPTY}")
    
    (new C).check()
    

    will say:

    @find() == EMPTY: true
    

    Demo: http://jsfiddle.net/ambiguous/7JGdq/

    So you need another way to check if toUpdate isn’t empty. You could count the properties in toUpdate:

    if (k for own k of toUpdate).length isnt 0
    

    or you could use the special EMTPY constant approach outlined above. There are various other ways to check for an empty object, Ricardo Tomasi​ has suggested a few:

    1. Underscore offers _.isEmpty which is basically the for loop approach with some special case handling and a short circuit.
    2. Underscore also offers _.values so you could look at _(toUpdate).values().length. This calls map internally and that will be the native map function if available.
    3. You could even go through JSON using JSON.stringify(toUpdate) is '{}', this seems a bit fragile to me and rather round about.
    4. You could use Object.keys instead of the for loop: Object.keys(toUpdate).length isnt 0. keys isn’t supported everywhere though but it will work with Node, up-to-date non-IE browsers, and IE9+.
    5. Sugar also has Object.isEmpty and jQuery has $.isEmptyObject.

    A short-circuiting for loop appears to be the quickest way to check emptiness:

    (obj) ->
        for k of toUpdate
            return true
        false
    

    That assumes that you don’t need own to avoid iterating over the wrong things. But given that this is just a test suite and that an emptiness test almost certainly won’t be a bottle neck in your code, I’d go with whichever of Underscore, Sugar, or jQuery you have (if you need portability and have to deal with the usual browser nonsense), Object.keys(x).length if you know it will be available, and (k for own k of toUpdate).length if you don’t have the libraries and have to deal with browser nonsense and aren’t certain that toUpdate will be a simple object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Getting JavaScript object key list How to iterate json data in jquery
Possible Duplicate: C++: What is the size of an object of an empty class?
Possible Duplicate: Hibernate: different object with the same identifier value was already associated with
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic According
Possible Duplicate: How do I test for an empty Javascript object from JSON? Is
Possible Duplicate: Hibernate: different object with the same identifier value was already associated with
Possible Duplicate: Rails primary key and object id Very quick question. My server is
Possible Duplicate: Hibernate: different object with the same identifier value was already associated with
Possible Duplicate: Loop through Json object { data: [ { name: Jen, id: 1
Possible Duplicate: Selecting a JSON object with a colon in the key I apologize

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.