简体   繁体   中英

check for null or undefined values in a an object of type array or type object

am testing the following scenarios

  1. if every value in an object is null or undefined it should return true
  2. if every value in an array is null or undefined values it should return true

my test is failing with an object who has nested array or object or an array who has nested array or objects

  describe('hasNullOrUndefined', () => {
    test.each`
      mockData                                                           | expected
      ${[{ a: null, b: null, c: null }, { a: null, b: null, c: null }]}  | ${true}
      ${{ a: null, a: null }}                                            | ${true}
      ${{ a: null, a: "a1"}}                                             | ${false}
      ${{ a: null, a: false }}                                 |         | ${false}
      ${[{ a: 'a1', b: 'b1', c: 'c1' }, { a: 'a2', b: 'b2', c: 'c2' }]}  | ${false}
      ${{ a: 'a1', b: 'b1', c: { a: 'a2', b: 'b2', c: 'c2' } }}          | ${false}
      ${{ a: null, b: null, c:[null,null] }}                             | ${true}
      ${{ a: null, b: null, c: {a: null, b: null} } }}                   | ${true}

      ${{}}                                                              | ${true}
      ${[null, null]}                                                    | ${true}
      ${[false, false]}                                                  | ${false}
      ${[true, true]}                                                    | ${false}
      ${[]}                                                              | ${true}
      ${[2259.93, 4259.99]}                                              | ${false}
      ${[, undefined]}                                                   | ${true}
    `(
      'should return the correct value $expected with mockData is $mockData',
      ({ mockData, expected }: TestCase<boolean>) => {
        const result = service.hasNullOrUndefined(mockData);
        expect(result).toEqual(expected);
      }
    );
  });

In my service

  public hasNullOrUndefined= (data: object | unknown[]): boolean => {
  
      if (this.isObject(data)) {
        return this.isObjectEmpty(data);
      } else {
        return Array.isArray(data) ? this.isArrayEmpty(data as []) : false;
      }
    
  };

  private isObject = (object: Object): boolean => {
    return Object.prototype.toString.call(object) === '[object Object]';
  };

  private isObjectEmpty = (object: Object): boolean => {
    return Object.keys(object).every(key => {
      return Array.isArray(object[key])
        ? this.isArrayEmpty(object[key])
        : Object.values(object).every(value => {
          if (value === null || value === undefined ) {
            return true;
          }
          return false;
        });
    });
  };

  private isArrayEmpty = (element: []): boolean => {
    return Object.keys(element).every(key => {
      return this.isObject(element[key])
        ? this.isObjectEmpty(element[key])
        : element[key] === null || element[key] === undefined ;
    });
  };

Thank you for your kind responses

You could check

  • if value is an array, then check every value,
  • if value is truthy and an object, then check all values or
  • check if value is null or undefined .
const
    isNullOrUndefined = value => {
        if (Array.isArray(value)) return value.every(isNullOrUndefined);
        if (!value && typeof value === 'object') return Object
            .values(value)
            .every(isNullOrUndefined);
        return value === null || value === undefined;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM