Skip to content

Commit

Permalink
feat(micro-dash): add isObject()
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Nov 11, 2020
1 parent 75e69ed commit 6495ec3
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"prenpm-publish": "yarn ts-node scripts/build-libs",
"npm-publish": "yarn ts-node scripts/publish-libs",
"precalc-micro-dash-sizes": "ng build --prod micro-dash",
"calc-micro-dash-sizes": "cd projects/micro-dash-sizes && yarn ts-node calc-sizes",
"ts-node": "ts-node -O \"{ \\\"module\\\": \\\"CommonJS\\\" }\""
"calc-micro-dash-sizes": "cd projects/micro-dash-sizes && ts-node --project tsconfig.app.json -O \"{ \\\"module\\\": \\\"CommonJS\\\" }\" calc-sizes",
"ts-node": "ts-node -O \"{ \\\"module\\\": \\\"commonjs\\\" }\""
},
"dependencies": {
"@angular/animations": "~10.2.2",
Expand Down
8 changes: 4 additions & 4 deletions projects/micro-dash-sizes/calc-sizes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import * as readline from 'readline';
import { explore } from 'source-map-explorer';
import { forEach } from '../micro-dash/src/lib/collection';

const appDir = path.join(__dirname, 'src', 'app');
const mainDir = path.join(__dirname, 'src');
const rootDir = path.join(__dirname, '..', '..');
const mainDir = path.join(__dirname, 'src');
const appDir = path.join(mainDir, 'app');
const bundleDir = path.join(rootDir, 'dist', 'micro-dash-sizes');
const sourceDir = path.join(rootDir, 'projects', 'micro-dash', 'src', 'lib');

Expand Down Expand Up @@ -74,7 +74,7 @@ async function build(inputPath: string): Promise<void> {
});
}

async function inspect(): Promise<string> {
async function inspect(): Promise<string | undefined> {
const res = await explore(path.join(bundleDir, 'main.*.js'));

let lodash = 0;
Expand All @@ -86,7 +86,7 @@ async function inspect(): Promise<string> {
microdash += size;
}
});
let summary: string;
let summary: string | undefined;
if (lodash > 0) {
summary = ` * - Lodash: ${lodash.toLocaleString()} bytes`;
} else if (microdash > 0) {
Expand Down
3 changes: 3 additions & 0 deletions projects/micro-dash-sizes/src/app/lang/is-object.lodash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import isObject from 'lodash-es/isObject';

console.log(isObject(1), isObject({}));
3 changes: 3 additions & 0 deletions projects/micro-dash-sizes/src/app/lang/is-object.microdash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { isObject } from '@s-libs/micro-dash';

console.log(isObject(1), isObject({}));
5 changes: 4 additions & 1 deletion projects/micro-dash-sizes/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/app",
"types": []
"types": [],

// Special just for micro-dash library functions, to reduce bundle size
"noImplicitReturns": false
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"]
Expand Down
1 change: 1 addition & 0 deletions projects/micro-dash/src/lib/lang/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { isFunction } from './is-function';
export { isMatch } from './is-match';
export { isNil } from './is-nil';
export { isNumber } from './is-number';
export { isObject } from './is-object';
export { isRegExp } from './is-reg-exp';
export { isString } from './is-string';
export { isUndefined } from './is-undefined';
Expand Down
39 changes: 39 additions & 0 deletions projects/micro-dash/src/lib/lang/is-object.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { isObject } from './is-object';

describe('isObject()', () => {
function getArguments(): IArguments {
return arguments;
}

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

it('should return `true` for objects', () => {
expect(isObject(getArguments())).toBe(true);
expect(isObject([1, 2, 3])).toBe(true);
expect(isObject(Object(false))).toBe(true);
expect(isObject(new Date())).toBe(true);
expect(isObject(new Error())).toBe(true);
expect(isObject(Array.prototype.slice)).toBe(true);
expect(isObject({ a: 1 })).toBe(true);
expect(isObject(Object(0))).toBe(true);
expect(isObject(/x/)).toBe(true);
expect(isObject(Object('a'))).toBe(true);
expect(isObject(document.body)).toBe(true);
expect(isObject(Object(Symbol('a')))).toBe(true);
});

it('should return `false` for non-objects', () => {
expect(isObject(null)).toBe(false);
expect(isObject(undefined)).toBe(false);
expect(isObject(false)).toBe(false);
expect(isObject(0)).toBe(false);
expect(isObject(NaN)).toBe(false);
expect(isObject('')).toBe(false);
expect(isObject(true)).toBe(false);
expect(isObject(1)).toBe(false);
expect(isObject('a')).toBe(false);
expect(isObject(Symbol('a'))).toBe(false);
});
});
10 changes: 10 additions & 0 deletions projects/micro-dash/src/lib/lang/is-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Checks if `value` is the [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types) of `Object`. _(e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)_
*
* Contribution to minified bundle size, when it is the only function imported:
* - Lodash: 105 bytes
* - Micro-dash: 41 bytes
*/
export function isObject(value: any): value is object {
return value instanceof Object;
}

0 comments on commit 6495ec3

Please sign in to comment.