From af636aae9699ff95885c72c3a81060ab225e92b5 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 4 Aug 2016 14:57:31 +0200 Subject: [PATCH] Add a parameter to the `isName` function that enables checking not just that something is a `Name`, but also that the actual `name` properties matches This is similar to the existing `isCmd` and `isDict` functions, which already support similar kind of checks. With the updated `isName` function, we'll be able to simplify many callsites from: `isName(someVariable) && someVariable.name === 'someName'` to: `isName(someVariable, 'someName')`. --- src/core/primitives.js | 14 ++++---------- test/unit/primitives_spec.js | 6 ++++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/core/primitives.js b/src/core/primitives.js index ee4d5e4ea..6a17e990a 100644 --- a/src/core/primitives.js +++ b/src/core/primitives.js @@ -267,8 +267,8 @@ var RefSetCache = (function RefSetCacheClosure() { return RefSetCache; })(); -function isName(v) { - return v instanceof Name; +function isName(v, name) { + return v instanceof Name && (name === undefined || v.name === name); } function isCmd(v, cmd) { @@ -276,14 +276,8 @@ function isCmd(v, cmd) { } function isDict(v, type) { - if (!(v instanceof Dict)) { - return false; - } - if (!type) { - return true; - } - var dictType = v.get('Type'); - return isName(dictType) && dictType.name === type; + return v instanceof Dict && + (type === undefined || isName(v.get('Type'), type)); } function isRef(v) { diff --git a/test/unit/primitives_spec.js b/test/unit/primitives_spec.js index 68cd86542..5e344b759 100644 --- a/test/unit/primitives_spec.js +++ b/test/unit/primitives_spec.js @@ -244,6 +244,12 @@ describe('primitives', function() { var name = Name.get('Font'); expect(isName(name)).toEqual(true); }); + + it('handles names with name check', function () { + var name = Name.get('Font'); + expect(isName(name, 'Font')).toEqual(true); + expect(isName(name, 'Subtype')).toEqual(false); + }); }); describe('isCmd', function () {