lodash-es#sortBy TypeScript Examples

The following examples show how to use lodash-es#sortBy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: searchee.ts    From cross-seed with Apache License 2.0 6 votes vote down vote up
function getFilesFromTorrent(meta: Metafile): File[] {
	if (!meta.info.files) {
		return [
			{
				name: meta.name,
				path: meta.name,
				length: meta.length,
			},
		];
	}

	const unsortedFiles = meta.info.files.map((file) => {
		const rawPathSegments: Buffer[] = file["path.utf-8"] || file.path;
		const pathSegments = rawPathSegments.map((s) => s.toString());
		return {
			name: pathSegments[pathSegments.length - 1],
			length: file.length,
			// Note that we don't use path.join here because of
			// https://github.com/mmgoodnow/cross-seed/issues/46.
			// path.join ignores zero-length path segments,
			// which we do not want.
			path: [meta.name, ...pathSegments].join(path.sep),
		};
	});

	return sortBy(unsortedFiles, "path");
}
Example #2
Source File: shuffle.spec.ts    From s-libs with MIT License 6 votes vote down vote up
describe('shuffle()', () => {
  it('accepts nil values for the collection, returning an empty array', () => {
    expect(shuffle(null)).toEqual([]);
    expect(shuffle(undefined)).toEqual([]);
  });

  //
  // stolen from https://github.com/lodash/lodash
  //

  const array = [1, 2, 3];
  const object = { a: 1, b: 2, c: 3 };

  it('should return a new array', () => {
    const actual = shuffle(array);
    expect(actual).toEqual(jasmine.arrayWithExactContents(array));
    expect(actual).not.toBe(array);
  });

  it('should contain the same elements after a collection is shuffled', () => {
    expect(shuffle(array).sort()).toEqual(array);
    expect(shuffle(object).sort()).toEqual(array);
  });

  it('should shuffle small collections', () => {
    const actual = times(1000, () => shuffle([1, 2]));
    expect(sortBy(uniqBy(actual, String), '0')).toEqual([
      [1, 2],
      [2, 1],
    ]);
  });
});
Example #3
Source File: to-pairs.spec.ts    From s-libs with MIT License 5 votes vote down vote up
describe('toPairs()', () => {
  it('does not consider `length` for an array', () => {
    expect(toPairs([1, 2, 3])).toEqual([
      ['0', 1],
      ['1', 2],
      ['2', 3],
    ]);
  });

  //
  // stolen from https://github.com/lodash/lodash
  //

  it('should create an array of string keyed-value pairs', () => {
    expect(sortBy(toPairs({ a: 1, b: 2 }), 0)).toEqual([
      ['a', 1],
      ['b', 2],
    ]);
  });

  it('should not include inherited string keyed property values', () => {
    function Foo(this: any): void {
      this.a = 1;
    }
    Foo.prototype.b = 2;

    expect(sortBy(toPairs(new (Foo as any)()), 0)).toEqual([['a', 1]]);
  });

  it('should convert objects with a `length` property', () => {
    const actual = sortBy(toPairs({ '0': 'a', '1': 'b', length: 2 }), 0);
    expect(actual).toEqual([
      ['0', 'a'],
      ['1', 'b'],
      ['length', 2],
    ]);
  });

  it('should return an array', () => {
    const array = [1, 2, 3];
    const actual = toPairs(array);

    expect(actual).toEqual(jasmine.any(Array));
    expect(actual).not.toBe(array as any);
  });
});